Threading in Java: मानलो कि हम चाहते हैं कि Applet पर एक Button हो जिस पर Click करने पर एक Musical Sound Play हो, जबकि बिना किसी व्यवधान के Applet पर एक Counter Number Increment हो कर Run होता रहे।
चूंकि Counter कुछ Milliseconds के लिए रूक कर Increment होगा और Applet फिर से Repaint होगा, इसलिए इस Counting Process को हमें Runnable Interface के run() Method में Implement करना होगा, जबकि Button पर Click करने पर Musical Sound के Play होने को Applet के एक Button से Associate करना होगा। किसी Applet में इस काम के लिए run() Method को हम निम्नानुसार Implement कर सकते हैं:
import java.awt.*; import java.applet.*; public class MyApplet extends Applet implements Runnable { String displayStr; int count = 0; public void run() { while (count < 1000) { ++count; displayStr = String.valueOf(count); repaint(); try { thread.sleep(200); } catch (InterruptedException e) {} } } public void paint(Graphics g) { g.drawString(displayStr, 100, 50); } }
इस run() Method में displayStr एक String Object है और count नाम का एक Integer प्रकार का Variable है। run() Method में हमने एक while Loop को 1000 बार Run किया है और इस Loop में count नाम के Integer प्रकार के Variable को Increment किया है। फिर count Variable के Integer प्रकार के मान को String Object में Convert करके displayStr नाम के String Object में Store किया है। फिर repaint() Method को Call किया है। repaint() Method Call करने पर Applet Class का paint() Method Execute होता है, जो displayStr String Object में Stored Counter के मान को Applet पर Display कर देता है।
चूंकि ये Statement एक while Loop में लिखा गया है, इसलिए displayStr में 1000 बार मान Change होता है और 1000 बार paint() Method Call होकर Counter को Applet पर Display करता है। लेकिन while Loop के हर Iteration में Thread के sleep() Method का Execution होता है और हर Iteration 200 Millisecond रूक कर Execute होता है।
हालांकि Applet के अन्दर हमने Runnable Interface को Implement करके एक Thread तो Create कर लिया है, लेकिन ये Thread तब तक Run नहीं होता है, जब तक Thread Super Class के start() Method को Call करके इस Thread के किसी Object का Reference नहीं दिया जाता है।
चूंकि किसी भी Thread को Run करने के लिए हमें सबसे पहले Thread Super Class का एक Object Create करना होता है और फिर उस Thread Object के Constructor में Argument के रूप में उस Thread की Class का Reference देना होता है, जिसमें Runnable Interface को Implement किया गया है।
चूंकि हमने Runnable Interface को Applet Class में ही Implement किया है, इसलिए Thread Super Class Object के Constructor में Argument के रूप में हम this Object Pass कर सकते हैं। चूंकि हमने Runnable Interface को Applet Class में ही Implement किया है, इसलिए this Object Applet Class को ही Refer करेगा।
हम चाहते हैं कि Applet के Memory में Load हो कर Start होते ही Thread भी Start हो जाए, इसलिए हमें Applet Class के start() Method को Override करना होगा और इसी Method में Thread Super Class के Constructor में Applet के this Object को Pass करके अपने Thread को Start करना होगा।
चूंकि जब हम Thread Super Class के Object में Applet को Refer करने वाले this Object का ही Reference देंगे, तो हमारे Applet के Thread का Reference Thread Super Class के Object में Store हो जाएगा। इसलिए हम Thread Super Class के start() Method को Thread Super Class के Object के लिए ही Invoke करके Applet के Thread को Start कर सकते हैं। यानी हमें Applet के start() Method को निम्नानुसार Override करना होगा:
Thread thread; public void start() { count = 0; displayStr = ""; thread = new Thread(this); thread.start(); }
हम जब Applet को Stop करते हैं, तभी हमारा Thread भी Terminate हो जाना चाहिए। इसलिए Thread को Store करने से सम्बंधित Code Statements को हमें Thread के stop() Method को Override करके निम्नानुसार लिखना होता है:
public void stop() { thread.stop(); }
इस Method को Override करके Thread को Stop करना इसलिए जरूरी है, क्योंकि Thread में एक Loop 1000 बार Execute होता है और हर Iteration में 200 Milliseconds का समय लेता है।
अब यदि हम Applet के Thread के Loop के Iterations पूरा होने से पहले Applet को Close करना चाहें, तो हमें Threads को बीच में ही रोकना जरूरी होता है। यदि हम ऐसा ना करें, तो Applet Terminate होने के बाद भी Applet का Thread Execute होता रहेगा और जब Thread के run() Method का Termination होगा, तभी Thread Terminate होगा। ऐसा होने पर CPU बेवजह वो काम करने में Busy रहेगा, जिसकी जरूरत ही नहीं है। इस तरह से इस पूरे Applet Program को हम निम्नानुसार एक साथ Define कर सकते हैं:
import java.awt.*; import java.applet.*; import java.net.*; public class SoundApplet extends Applet implements Runnable{ Button button; Thread threada; int count; String displayStr; public void init() { setLayout(null); button = new Button("Play Sound"); add(button); button.setVisible(true); button.setBounds(5,80,250,60); resize(260, 150); Font font = new Font("TimesRoman", Font.BOLD, 50); setFont(font); } public void start(){ count = 0; displayStr = ""; thread = new Thread(this); thread.start(); } public void stop(){ thread.stop(); } public void run(){ while (count < 1000){ ++count; displayStr = String.valueOf(count); repaint(); try{ thread.sleep(200); }catch (InterruptedException e) {} } } public void paint(Graphics g){ g.drawString(displayStr, 100, 50); } public boolean action(Event evt, Object arg){ if (evt.target instanceof Button){ play(getCodeBase(), "spacemusic.au"); repaint(); } return true; } }

ये Article इस वेबसाईट पर Selling हेतु उपलब्ध EBook Java in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी रहा, तो निश्चित रूप से ये पुस्तक भी आपके लिए काफी उपयोगी साबित होगी।
Java Programming Language in Hindi | Page: 682 | Format: PDF