Java Multi-threading

Introduction to Java Multi-threading

सब phone पर बात करते हुए खाना खा सकते है और साथ ही TV भी देख सकते है। हमारा दिमाग इस तरह प्रोग्राम किया हुआ है की हम एक साथ कई काम कर सकते है। इसे multitasking कहा जाता है। इसी तरह java का कोई भी प्रोग्राम एक साथ कई टास्क complete कर सकता है। Java के इस feature को multi-threading कहते है।

उदाहरण के लिए जब आपका प्रोग्राम user input के लिए wait कर रहा है तो इस समय में वह दूसरा कोई काम complete कर सकता है। जैसे ही database से connectivity की जा सकती है या फिर किसी file को upload किया जा सकता है।

एक multi-threading प्रोग्राम के 2 या 2 से अधिक parts होते है। ये parts एक साथ execute किये जा सकते है। हर part thread कहलाता है। हर thread एक unique task complete करने के लिए responsible होती है। ये सभी threads main thread में होती है। आपके program की main थ्रेड main() method के साथ शुरू होती है।

हर thread की एक life cycle होती है। इस life cycle में thread कई stages से गुजरती है। जब आप thread क्लास का object क्रिएट करते है, तो सबसे पहले एक thread new state में आती है। इसके बाद जब thread object पर start() मेथड कॉल किया जाता है तो thread runnable state में पहुँच जाती है। इसके बाद जब run() method कॉल होता है तो thread running state में होती है। इसके बाद ये thread को execution के लिए wait करना पड़े तो thread waiting state में पहुंच जाती है। और जब thread का execution complete हो जाता है तो तो thread destroyed stage में पहुँच जाती है।

Java में multi-threading 2 तरह से implement की जा सकती है। पहले तरीके में आप thread क्लास को extend करते है। दूसरे तरीके में आप Runnable interface को implement करते है। जब आप चाहते है की आप run() मेथड के अलावा दूसरे methods भी program में यूज़ करे तो आप thread क्लास को extend कर सकते है। और यदि आप सिर्फ run() override करना चाहते है तो आप Runnable interface implement कर सकते है।

Thread Class Implementation

Thread class बहुत सारे methods provide करती है। लेकिन thread class को implement करने के लिए आपको इन सभी methods को override करने की आवश्यकता नहीं है। आप सिर्फ run() मेथड override कर सकते है। Thread class के कुछ important methods निचे दिए जा रहे है।

Method Description
getName() Returns name of thread.
getPriority() Returns priority of thread.
isAlive() Returns true if thread is active.
join() Joins two threads.
run() Threads task is defined in this method.
sleep() Make a sleep for give time in milliseconds.
start() Starts a thread.

सबसे पहले आपको thread class को extend करना होगा। इसके बाद आप अपनी क्लास में run method को override करते है। यदि आप चाहे तो और भी methods को override कर सकते है। लेकिन run() method को override करना necessary है। इसके बाद आप अपनी इस class का object create करते है। Object create करते समय आप चाहे तो thread का एक unique नाम भी पास कर सकते है।

Object create करने के बाद आप उस object पर start() method कॉल करते है। start() मेथड automatically run() method को call करता है, और आपके thread का execution start हो जाता है। आप जितनी threads create करना चाहते है उतने ही object create करते है।

class JavaHindi extends thread
{
    public static void main(String args[])
    {
         JavaHindi jh = new JavaHindi();
         jh.start();  
    }

    Public void run()
    {
          System.out.println(“Thread is running at www.besthinditutorials.com”);
    }
}

Runnable Interface Implementation

Runnable interface में एक ही method होता है। run() method की definition आपको अपनी class में provide करनी होती है। सबसे पहले आप एक class create करते है, जो run() मेथड को implement करती है। इसके बाद आप Thread class का object क्रिएट करते है। इस thread क्लास के object में आप Runnable interface को implement करने वाली class का object और thread का नाम पास करते है। और उसके बाद आप thread class के object पर start() method को call करते है।

जब आप Runnable interface को implement करने वाली क्लास का object thread क्लास के object में pass करते है तो thread क्लास का object उसी class के object को point करता है। इसका उदाहरण नीचे दिया जा रहा है।

class JavaHindi implements Runnable
{
    JavaHindi()
    {
        Thread t = new Thread(this,HindiThread);
        t.start();
    }   

    Public void run()
    {
          System.out.println(“Thread is running at www.besthinditutorials.com”);
    }

    public static void main(String args[])
    {
         JavaHindi jh = new JavaHindi();
    }
}

Synchronization

जब 2 या 2 से अधिक thread किसी एक resource (method) पर access चाहती है, तो conflict पैदा होता है। इस conflict से बचने के लिए आप एक technique यूज़ करते है। इस technique से एक समय पर एक ही thread वो resource access कर सकती है। इस technique को synchronization कहते है।

Synchronization की technique में आप किसी method को synchronized बना देते है। इसके लिए आप उस मेथड की definition से पहले synchronized method add कर देते है। जैसे की synchronized void myMethod(){}.

Synchronization के concept को समझने के लिए आपको monitor का concept समझना होगा। हर object के साथ उसका monitor जुड़ा होता है। जब कोई thread synchronized मेथड को call करती है, तो वो थ्रेड monitor में enter हो जाती है। जैसे ही thread monitor में enter होती है उस resource पर lock लग जाता है। अब जब तक ये thread इस resource को free नहीं करेगी तब तक दूसरी कोई भी thread इस resource को access नहीं कर सकती है।

किसी भी resource को synchronized बनाने के 2 तरीके है। एक तो जैसे की मैने आपको बताया आप method के आगे synchronized keyword लगा सकते है और उसे synchronized बना सकते है। दूसरे तरीके में आप एक synchronized block create करते है। और इस block में उस class के methods को कॉल करते है, जिनको आप synchronized बनाना चाहते है। इसका उदाहरण निचे दिया जा रहा है।

Synchronized(s1) // s1 is the object of class of which methods you are going to call
{
   // call here methods that you want to make synchronized
}

Exams और interviews के point of view से java multi-threading एक बहुत ही important concept है।