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 अलग-अलग पड़ावों से गुजरता है |
- New State
- Runnable State
- Running State
- Waiting State
- 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 :Output :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()); } }
t1 State : NEW t2 State : NEW t1 State : RUNNABLE run method t2 State : NEW t1 State : TERMINATED run method t2 State : RUNNABLE