C# Multithreading

Introduction to C# Multithreading

Multithreading ऐसी programming technique है जिसमें multiple threads का प्रयोग करते हुए कई code blocks को एक साथ execute किया जाता है।

Thread एक execution path होती है जो आपके program को operating system से connect करने के लिए और उसके resources (CPU आदि) का प्रयोग करने के लिए provide की जाती है।

Normally जब आप एक C# program execute करते है तो वह एक thread के द्वारा ही execute होता है। यह thread CLR द्वारा program को run करने से पूर्व create की जाती है। इसे main thread कहा जाता है।

ऐसी applications जो सिर्फ एक ही thread के द्वारा execute होती है वे single threaded applications कहलाती है। Single threaded applications का drawback यह होता है की उनमें एक task के complete होने पर ही दूसरा task execute हो सकता है।

लेकिन कई बार आपको एक से अधिक tasks एक साथ execute करने की आवश्यकता हो सकती है। ऐसे में आप multithreading का प्रयोग कर सकते है। Multithreading technique द्वारा अपने program में एक से अधिक threads create करके आप अलग अलग code blocks को (अलग अलग execution paths के द्वारा) एक साथ execute कर सकते है।

Main thread के अलावा आप जो threads create करते है वे child threads कहलाती है। यदि main thread का execution complete हो जाता है तो वह terminate होने से पहले child threads का execution complete होने तक wait करती है।

Thread Class

C# में multithreading implement करने के लिए Threading namespace provide किया गया है। इस namespace में कई classes, structs, delegates, Enumerators और interfaces provide किये गए है जिनके द्वारा multithreading implement करना आसान होता है।

अपने program में multithreading implement करने के लिए आपको इस namespace को शुरआत में include करना होता है। इसका syntax निचे दिया जा रहा है।

using System.Threading;

Threading namespace में provide की गयी Thread class सबसे महत्वपूर्ण है। इस class का object create करके ही आप threads create करते है। यह class threads को manage करने के लिए कई methods और properties provide करती है।

Thread class में provide किये गए सर्वाधिक प्रयोग होने वाले कुछ methods के बारे में निचे बताया जा रहा है।

  1. Start() – यह method thread को start करने के लिए प्रयोग किया जाता है। इस method को thread class के object के साथ call किया जाता है।
  2. Sleep() – यह method thread को कुछ समय के लिए pause करने के लिए प्रयोग किया जाता है। इस method में समय miliseconds के रूप में pass किया जाता है।
  3. Suspend() – इस method का प्रयोग thread को suspend करने के लिए किया जाता है। यदि कोई thread पहले से ही suspended state में है तो इस method का कोई effect नहीं होता है। Suspend की गयी thread को दुबारा resume किया जा सकता है।
  4. Abort() – इस method का प्रयोग किसी thread को abort करने के लिए किया जाता है। इस method में आप argument के रूप में string message pass करते है जो thread के abort होने के कारण के रूप में show किया जा सकता है।
  5. Resume() – इस method का प्रयोग किसी suspend की गयी thread को resume करने के लिए किया जाता है।
  6. Join() – इस method का प्रयोग किसी thread को दूसरी thread के साथ जोड़ने के लिए किया जाता है। ऐसा करने से जब तक वह thread finish नहीं हो जाती दूसरी thread block कर दी जाती है।

जैसा की मैने पहले बताया Thread class में properties भी provide की गयी है। कुछ useful properties के बारे में निचे बताया जा रहा है।

  1. isAlive – यदि thread alive (running) है तो इस property की value true होती है।
  2. isBackground – इस property के द्वारा thread को background property के रूप में set किया जा सकता है। इसके अलावा इस property के द्वारा यह भी पता लगाया जाता है की कोई thread background है या नहीं।
  3. Name – इस property के द्वारा thread का नाम get (access) और set किया जाता है।
  4. Priority – इस property के द्वारा आप thread की priority get और set कर सकते है।
  5. ThreadState – यह property thread की current state को describe करती है। एक thread un-started, runnable, running, not runnable और dead में से किसी state में हो सकती है।

Creating & Managing Threads

C# में thread create करने के लिए Thread class का object create किया जाता है। Thread class का हर object एक separate thread को represent करता है।

C# में thread create करने का general syntax निचे दिया जा रहा है।

Thread thread-obj-name = new Thread(ThreadStart-class-reference);

जैसा की आप ऊपर दिए गए syntax में देख सकते है Thread class का object create करते समय उसमे argument के रूप में ThreadStart class का reference pass किया जाता है। यह reference उस function को point करता है जो उस thread के start होने पर execute किया जाएगा।

ThreadStart class का reference pass करने का general syntax निचे दिया जा रहा है।

new ThreadStart(function-name);

जैसा की आप ऊपर दिए गए syntax में देख सकते है ThreadStart Class का reference directly new keyword के प्रयोग से pass किया जाता है। यह reference pass करने की shorter form होती है। आप चाहे तो ThreadStart class का object create करके उसमे function pass करके उस object को Thread class का object create करते समय pass कर सकते है।

लेकिन ऐसा करने से आपको ThreadStart class के अलग से objects create करने होंगे जिससे code काफी lengthy हो जाएगा। इसलिए आपको हमेशा इस shorter form को ही use करना चाहिए। इसे आसानी से Thread class object create करते समय pass किया जा सकता है।

इस reference में उस function का नाम pass किया जाता है जो thread के start होने पर execute किया जाएगा।

अलग अलग threads को create करते समय आप अलग अलग functions के नाम उनमें pass करते है। इन functions को normal functions की तरह ही create किया जाता है। इन functions में आप वह code लिखते है जो किसी particular thread द्वारा execute किया जाएगा।

इस प्रकार start होने पर अलग अलग threads द्वारा अलग अलग functions call किये जाते है जो अलग अलग tasks perform करते है।

Thread class का object create करने के बाद thread को start करना होता है। Thread को start करने के लिए Thread object से start() method call किया जाता है। इसका syntax निचे दिया जा रहा है।

thread-obj-name.Start();

Thread को start करने के बाद उसके execution को आप Thread class द्वारा provide किये गए methods द्वारा manage कर सकते है।

Steps to Create a Multithreaded Program

एक multithreaded program create करने की step by step process निचे दी जा रही है।

  1. System और System.Threading namespaces के लिए using statement define कीजिये।
  2. Class और Main() method define कीजिये।
  3. Methods define कीजिये जो विभिन्न threads के शुरू होने पर execute होंगे। इन methods में आप main functionality के अलावा threads को control करने के लिए sleep(), Suspend() आदि methods भी प्रयोग कर सकते है।
  4. आप जितनी threads program में create करना चाहते है उतने Thread class के object create कीजिये।
  5. Threads को start करने के लिए हर Thread objects के साथ Start() method call कीजिये।

Example of C# Multithreading

C# में multithreading के उपयोग को निचे उदाहरण द्वारा समझाया जा रहा है।

using System;
using System.Threading;

class threadDemo
{
     static void Main(string[] args)
     {
          Thread t1 = new Thread(new ThreadStart(ThreadFunc));
          Thread t2 = new Thread(new ThreadStart(ThreadFunc));
          t1.Start();
          t2.Start();
     }

     public static void ThreadFunc()
     {
          for(int i=1;i<=10;i++)
          {
               Thread.Sleep(100);
               Console.WriteLine(i);
          }
     }
}

ऊपर दिया गया उदाहरण निचे दिया गया output generate करता है।

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10