Threads in Java ๐Ÿงถ

3 min read

Cover Image for Threads in Java ๐Ÿงถ

What are Threads in Java? ๐Ÿงต (Simple Explanation)

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:

  1. By extending theThreadclass:

    • It's like writing a recipe ๐Ÿ“ for your helper.

    • You write a class that extends Thread and put the instructions in the run() method.

  2. By implementing theRunnableinterface:

    • This is like giving a list of tasks ๐Ÿ“‹ to an existing helper.

    • You write a class that implements Runnable and put the tasks in the run() method. Then you give this list to a Thread object.

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 Email
class EmailSender implements Runnable {
    public void run() {
        // Code to send an email
        System.out.println("Email sent! ๐Ÿ“ง");

// Task 2: Calculate Sum
class 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

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 ๐Ÿคน.