आपकी ऑफलाइन सहायता

BACK
49

सी प्रोग्रामिंग

149

पाइथन प्रोग्रामिंग

49

सी प्लस प्लस

99

जावा प्रोग्रामिंग

149

जावास्क्रिप्ट

49

एंगुलर जे.एस.

69

पी.एच.पी.
माय एस.क्यू.एल.

99

एस.क्यू.एल.

Free

एच.टी.एम.एल.

99

सी.एस.एस.

149

आर प्रोग्रामिंग

39

जे.एस.पी.





डाउनलोड पी.डी.एफ. ई-बुक्स
Java - Multithreading

Introduction for Multithreading

एक साथ अनेक threads या sub-programs को run किया जाना Multithreading होता है |

Java में Multithreading का program एक साथ एक से ज्यादा threads को run करता है | ये thread बहुत ही light-weight की process होती है |

अगर Multithreading का उदाहरण ले तो जब MS Word में कोई data लिखते वक्त spell checks होता है, ये process MS Word के background पर होता है |

Thread क्या है ?

  • thread ये एक process का sub-process है |
  • thread light-weight होते है |
  • program में एक या एक से ज्यादा threads create किये जा सकते है |
  • जब process पर कोई thread create नहीं किया जाता तो main thread create होता है |
  • thread; process की common memory area को share करता है | thread अलग से memory को allocate नहीं करता |

Life Cycle for Thread

threads का एक Life Cycle है | threads अलग-अलग पड़ावों से गुजरता है |

  1. New State
  2. Runnable State
  3. Running State
  4. Waiting State
  5. Dead State


New State : यहाँ से thread की शुरुआत होती है | यहाँ पर thread का objet create किया जाता है |

Ready/Runnable State : जब start() method को call किया जाता है, तब thread New से Runnable State पर आ जाता है | ये execute होने के लिए Ready होता है |

Running State : जब thread execution होना शुरू होता है, तब thread इस state पर होता है |

Waiting State : दुसरे thread को perform करने के लिए कुछ thread को block या waiting state पर होते है, waiting state पर जो thread होता है उसे resume किया जाता है |

Dead State : यहाँ पर thread का काम पूरा होकर वो बंद हो जाता है |


Example for Thread States

Source Code :
public class CheckState extends Thread{
	
	public void run() {
		System.out.println("run method");
  }
  public static void main(String args[]){
	  
	CheckState t1 = new CheckState();
	CheckState t2= new CheckState();
	
	System.out.println("t1 State : " + t1.getState());
	System.out.println("t2 State : " + t2.getState());
		
	t1.start();
	System.out.println("t1 State : " + t1.getState());
	System.out.println("t2 State : " + t2.getState());

	t2.start();
	System.out.println("t1 State : " + t1.getState());
	System.out.println("t2 State : " + t2.getState());
    }
}
Output :
t1 State : NEW
t2 State : NEW
t1 State : RUNNABLE
run method
t2 State : NEW
t1 State : TERMINATED
run method
t2 State : RUNNABLE