📘 Lesson · Lesson 35
Multithreading
Multithreading
What is Multithreading?
Multithreading lets a program run multiple tasks at the same time using threads — improving speed and responsiveness.
Creating a Thread
class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
}
MyThread t = new MyThread();
t.start(); // starts a new thread
// Or with Runnable
Runnable task = () -> System.out.println("Task running");
new Thread(task).start();
Thread Lifecycle
New → Runnable → Running → (Waiting/Blocked) → Terminated. start() moves it to Runnable; the scheduler runs it.
Summary
- Multithreading runs tasks concurrently via Thread or Runnable.
- Call
start()(not run()) to begin a new thread.
Multithreading क्या है?
Multithreading program को threads से कई tasks एक साथ चलाने देता है — speed और responsiveness बढ़ाता है।
Thread बनाना
class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
}
MyThread t = new MyThread();
t.start(); // नया thread शुरू करता है
// या Runnable से
Runnable task = () -> System.out.println("Task running");
new Thread(task).start();
Thread Lifecycle
New → Runnable → Running → (Waiting/Blocked) → Terminated। start() इसे Runnable करता है; scheduler चलाता है।
सारांश
- Multithreading Thread या Runnable से tasks एक साथ चलाता है।
- नया thread शुरू करने को
start()call करें (run() नहीं)।