Threads in Java
What are Threads in Java?
Imagine you’re in a kitchen cooking a meal. You’re the chef (the main program), and you have several tasks like chopping vegetables, boiling water, and frying eggs. In a normal scenario (single-threaded), you’d do each task one after the other. But what if you could have helpers (threads) who could do some of these tasks at the same time? That’s exactly what threads in Java do!
In Java, a thread is like a helper in your program. It can run parts of your code in parallel with other parts. This means you can do multiple things at the same time, like playing music while downloading a file.
How to Create Threads
There are mainly two ways to create a thread in Java:
-
By extending the
Threadclass:- It’s like writing a recipe for your helper.
- You write a class that extends
Threadand put the instructions in therun()method.
-
By implementing the
Runnableinterface:- This is like giving a list of tasks to an existing helper.
- You write a class that implements
Runnableand put the tasks in therun()method. Then you give this list to aThreadobject.
A Simple Example
Let’s say you’re writing a program that sends an email and at the same time calculates the sum of numbers.
Implementing Runnable Interface
// Task 1: Sending an Emailclass EmailSender implements Runnable { public void run() { // Code to send an email System.out.println("Email sent!"); }}
// Task 2: Calculate Sumclass SumCalculator implements Runnable { public void run() { int sum = 0; for (int i = 1; i <= 10; i++) { sum += i; } System.out.println("Sum is: " + sum); }}
public class Main { public static void main(String[] args) { // Create tasks EmailSender emailSender = new EmailSender(); SumCalculator sumCalculator = new SumCalculator();
// Create threads Thread thread1 = new Thread(emailSender); Thread thread2 = new Thread(sumCalculator);
// Start threads thread1.start(); thread2.start(); }}In this example, EmailSender and SumCalculator are like two separate helpers. The Main class creates these helpers and starts them. They run simultaneously, so the email can be sent while the sum is being calculated.
Benefits of Using Threads
- Efficiency: Your program can do multiple things at once, like a chef who can cook and talk on the phone simultaneously.
- Better Use of Resources: If you have to wait for something (like waiting for a file to download), you can do other tasks in the meantime.
Things to Keep in Mind
- Thread Safety: When threads share resources (like ingredients in a kitchen), you need to be careful. If not handled properly, it can lead to problems (like two helpers using the same frying pan at the same time).
- Complexity: Using threads can make your program more complex, like managing a kitchen with many helpers. You need to coordinate them well.