Java Multithreading जावा में मल्टीथ्रेडिंग

Multithreading in Java is a powerful feature that allows concurrent execution of two or more parts of a program to make the best use of CPU. Java enables programs to perform multiple tasks simultaneously using threads.

जब हम जावा प्रोग्राम चलाते हैं, तो CPU को कई बार इंतजार करना पड़ता है जैसे – इनपुट लेने में, नेटवर्क रिस्पॉन्स का इंतजार, या कोई अन्य प्रोसेस के कारण। इस बेकार समय का उपयोग हम दूसरे कार्यों के लिए कर सकते हैं, और यही प्रक्रिया Multithreading कहलाती है।


🧵 What is a Thread? (थ्रेड क्या होता है?)

A Thread is a lightweight subprocess, the smallest unit of processing. A thread executes within a program and shares process resources with other threads.

एक थ्रेड वह यूनिट है जो "execution में चल रहा प्रोग्राम" कहलाता है। जावा प्रोग्राम में default thread main() होता है। लेकिन हम और भी थ्रेड्स बना सकते हैं ताकि काम समानांतर (parallel) हो सके।


🔄 Thread Life Cycle (थ्रेड का जीवन चक्र)

  1. New – Thread is created. थ्रेड ऑब्जेक्ट बनते ही इस स्थिति में होता है।

  2. Runnable – Thread is ready to run. start() मेथड कॉल होने पर Runnable स्थिति में आता है।

  3. Running – Thread is executing.  run() मेथड कॉल होने पर यह एक्टिव होता है।

  4. Waiting – Thread is waiting for resources.  किसी कारण थ्रेड रुका हुआ है।

  5. Terminated – Thread has completed execution. कार्य समाप्त होते ही थ्रेड Destroy हो जाता है।


🔧 Creating Thread in Java (जावा में थ्रेड बनाना)

1️⃣ Using Thread Class (थ्रेड क्लास का उपयोग)

English:
You can create a thread by extending the Thread class and overriding the run() method.

Hindi:
आप Thread क्लास को extend करके और run() मेथड को override करके थ्रेड बना सकते हैं।

🧪 Example Program:

// Example using Thread class
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running using Thread class.");
}
public static void main(String args[]) {
MyThread t1 = new MyThread();
t1.start(); // Start the thread
}
}

Output:

Thread is running using Thread class.

2️⃣ Using Runnable Interface (रनेबल इंटरफेस का उपयोग)

English:
You can implement the Runnable interface and pass it to the Thread constructor.

Hindi:
आप Runnable इंटरफेस को implement करके उसे Thread के कंस्ट्रक्टर में पास कर सकते हैं।

🧪 Example Program:

// Example using Runnable interface
class MyRunnable implements Runnable {
public void run() {
System.out.println("Thread is running using Runnable interface.");
}
public static void main(String args[]) {
MyRunnable myRunnable = new MyRunnable();
Thread t1 = new Thread(myRunnable);
t1.start(); // Start the thread
}
}

Output:

Thread is running using Runnable interface.

🛠️ Important Thread Methods (महत्वपूर्ण थ्रेड मेथड्स)

Method Description (English) विवरण (Hindi)
start() Starts the thread थ्रेड को चालू करता है
run() Contains the code to be executed थ्रेड द्वारा किया जाने वाला कार्य
sleep(ms) Puts thread to sleep for milliseconds थ्रेड को कुछ समय तक सुलाता है
join() Waits for another thread to die दूसरे थ्रेड के समाप्त होने का इंतजार
getName() Gets thread name थ्रेड का नाम प्राप्त करता है
getPriority() Gets thread priority थ्रेड की प्राथमिकता दर्शाता है
isAlive() Checks if thread is alive थ्रेड ज़िंदा है या नहीं

🧠 Why Use Multithreading in Java? (जावा में मल्टीथ्रेडिंग क्यों?)

✅ Better CPU Utilization (बेहतर CPU उपयोग)
✅ Faster Execution (तेजी से एक्सीक्यूशन)
✅ Efficient Resource Handling (प्रभावी संसाधन उपयोग)
✅ Useful for Games, Servers, GUI Apps (खासकर गेम, सर्वर और GUI के लिए)


📝 निष्कर्ष (Conclusion)

Multithreading makes Java powerful and efficient. By creating multiple threads, a program can utilize system resources better and provide faster execution.

मल्टीथ्रेडिंग से जावा और भी ज्यादा प्रभावी और शक्तिशाली बनता है। यह सीपीयू के समय का बेहतर उपयोग करता है और आपकी एप्लिकेशन को responsive बनाता है।

Comments

Popular posts from this blog

What is a Web Browser? वेब ब्राउज़र क्या है?

Java's Support System जावा का सहयोगी तंत्र

The Internet and Java इंटरनेट और जावा का सम्बन्ध