Multi threading का परिचय (Introduction of Multi-Threading)
Multi threading
का
परिचय
(Introduction of
Multi-Threading)
Computer पर बैठ कर आप
गाना सुन सकते हैं, अपनी पसंद कि मूवी देख सकते हैं, गेम खेल सकते हैं , अपना काम कर
करते हैं यानि कि आप कंप्यूटर को multitasking कह सकते हैं क्यूंकि आप एक साथ कंप्यूटर
पर एक से ज्यादा काम कर सकते हैं
इसी
प्रकार आप जावा के एक प्रोग्राम में एक से ज्यादा task और operation परफॉर्म कर
सकते हैं | जावा के इसी feature को multi-threading कहते हैं
उदाहरण
के तौर पर
एक
multi-threading प्रोग्राम में एक से ज्यादा parts होते हैं जो अलग अलग task
perform करते हैं जैसे कि एक part addition कर रहा है, दूसरा part substract कर
रहा है और तीसरा part multiply कर रहा है आदि | प्रोग्राम का हर part एक thread
कहलाता है और हर thread एक unique task perform करता है और ये सभी threads main
thread के अंदर होते हैं और प्रोग्राम में main thread , प्रोग्राम के main
method() के साथ शुरू होती है |
Thread क्या
है?
·
thread एक process का sub process और प्रोग्राम
का एक task होता हैं
·
thread light weight होते हैं क्यूंकि प्रोग्राम
में एक से ज्यादा thread हो सकते हैं
·
प्रोग्राम में एक से ज्यादा thread create किये
जा सकते हैं
·
thread process की ही memory को इस्तेमाल करता
है thread के लिए कोई अलग से memory को allocate नहीं किया जाता है
Life-Cycle of Thread
Program के अंदर thread
को बहुत सारी steps से होकर गुजरना पड़ता है जो नीचे दिए गए हैं –
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
का काम पूरा होकर वो बंद हो जाता है |
Benefits
of threads
1-
Responsiveness:- अगर एक process
को एक से ज्यादा threads में divide कर दिया जाता है तो और जब एक thread अपने आप
को execute कर लेता है तो उसके output को जल्द से जल्द show किया जा सकता है
2-
Effective
Utilization of Multiprocessor System :- जब हम एक प्रोसेसर में एक से जयादा thread में काम बाँट कर उस काम के
execute होने की गति को बड़ा सकते है तो सोचिये अगर आपके पास multi-processor हो तो
उसके process की execution की गति कितनी बाद जाएगी |
3-
Resource sharing :- आप अपने
कंप्यूटर की resources जैसे की फाइल ,कोड को आप अपने process के threads के बीच
में शेयर कर सकते हैं
4-
Communication :-
multi-threading के बीच में communication आसानी से हो जाता है क्यूंकि process के
सभी threads एक ही address और एक ही memory को शेयर करते हैं |
5-
Enchanced Throughput of the system :- अगर
एक process को multi threading में बनाया गया है तो इसका मतलब यह है कि हर एक thread के पास अपना एक task है
और process के सभी thread एक ही समय पर अपना task complete कर सकते हैं जिससे
सिस्टम के output की संख्या बाद जाती है |
Example
of Thread
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());
}
}
Multi threading का परिचय (Introduction of Multi-Threading)
Reviewed by Unknown
on
October 21, 2018
Rating:
प्रोग्रामर के लिए जावा भाषा का नमूना कोड
ReplyDeleteसर्वलेट इनिशिएटिव पैरामीटर जावा कोड