Multithreading is a way to make your Java programs do multiple things at the same time. Imagine you are doing your homework while listening to music. You are doing two tasks at once. In Java, we can use threads to do this.
A thread is like a small part of a program that can run independently. When you run a Java program, it starts with one main thread. You can create more threads to do other tasks at the same time.
- Efficiency: It makes your program run faster by doing multiple tasks at once.
- Responsiveness: It keeps your program responsive. For example, a game can keep running while it loads new levels in the background.
There are two main ways to create threads in Java:
- Extending the
Threadclass - Implementing the
Runnableinterface
public class MyThread extends Thread {
public void run() {
System.out.println("This is a thread running.");
}
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start(); // Start the thread
}
}In this example, we create a new class MyThread that extends the Thread class. We override the run method to define what the thread will do. Then, we create an instance of MyThread and call the start method to run the thread.
public class MyRunnable implements Runnable {
public void run() {
System.out.println("This is a thread running.");
}
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start(); // Start the thread
}
}In this example, we create a new class MyRunnable that implements the Runnable interface. We override the run method to define what the thread will do. Then, we create an instance of MyRunnable and pass it to a new Thread object. Finally, we call the start method to run the thread.
Sometimes, multiple threads need to share resources, like a variable or a file. If they try to use the resource at the same time, it can cause problems. Thread synchronization helps to prevent these problems.
class Counter {
private int count = 0;
public synchronized void increment() {
count++;
}
public int getCount() {
return count;
}
}
public class SyncExample {
public static void main(String[] args) {
Counter counter = new Counter();
Thread t1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
counter.increment();
}
});
Thread t2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
counter.increment();
}
});
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Count: " + counter.getCount());
}
}In this example, we have a Counter class with a synchronized increment method. This ensures that only one thread can increment the count at a time. We create two threads that both increment the counter 1000 times. By using join, we wait for both threads to finish before printing the final count.
Multithreading allows your Java programs to do multiple tasks at the same time. You can create threads by extending the Thread class or implementing the Runnable interface. Thread synchronization helps to manage shared resources safely. With these tools, you can make your programs more efficient and responsive.
Happy coding!
<< Previous | Home | Next >>