Java Threads

Threads are a fundamental feature of Java, enabling developers to perform multiple tasks concurrently. In this guide by The Coding College, we’ll explore threads, their importance in Java programming, and how to use them effectively.

What is a Thread?

A thread is a lightweight subprocess that allows a program to execute tasks concurrently. Threads are essential for improving application performance, especially in scenarios requiring multitasking.

For example:

  • Processing large data sets while allowing user interaction.
  • Downloading files without freezing the UI.

The java.lang.Thread Class

Java provides built-in support for threads through the Thread class and the Runnable interface.

Thread Life Cycle:

  1. New: The thread object is created.
  2. Runnable: The thread is ready to run.
  3. Running: The thread is executing.
  4. Blocked/Waiting: The thread is waiting for a resource or condition.
  5. Terminated: The thread has completed execution.

Creating Threads in Java

There are two primary ways to create threads in Java:

1. Extending the Thread Class

class MyThread extends Thread {
    public void run() {
        System.out.println("Thread is running");
    }
}

public class Main {
    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start(); // Start the thread
    }
}

Output:

Thread is running

2. Implementing the Runnable Interface

class MyRunnable implements Runnable {
    public void run() {
        System.out.println("Thread is running");
    }
}

public class Main {
    public static void main(String[] args) {
        MyRunnable runnable = new MyRunnable();
        Thread thread = new Thread(runnable);
        thread.start(); // Start the thread
    }
}

Output:

Thread is running

Thread Methods

MethodDescription
start()Starts the thread and calls the run() method.
run()Defines the task for the thread to execute.
sleep(ms)Pauses the thread for a specified time (ms).
join()Waits for a thread to finish execution.
isAlive()Checks if a thread is still running.
setPriority()Sets the priority of a thread.

Example: Using Multiple Threads

class MyThread extends Thread {
    private String threadName;

    MyThread(String name) {
        threadName = name;
    }

    public void run() {
        for (int i = 1; i <= 5; i++) {
            System.out.println(threadName + ": " + i);
            try {
                Thread.sleep(500); // Pause for 500ms
            } catch (InterruptedException e) {
                System.out.println(e.getMessage());
            }
        }
    }
}

public class Main {
    public static void main(String[] args) {
        MyThread t1 = new MyThread("Thread 1");
        MyThread t2 = new MyThread("Thread 2");

        t1.start();
        t2.start();
    }
}

Output:

Thread 1: 1
Thread 2: 1
Thread 1: 2
Thread 2: 2
...

Thread Synchronization

When multiple threads access shared resources, synchronization is required to prevent data inconsistencies.

Synchronized Method Example

class SharedResource {
    synchronized void printNumbers(String threadName) {
        for (int i = 1; i <= 5; i++) {
            System.out.println(threadName + ": " + i);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                System.out.println(e.getMessage());
            }
        }
    }
}

class MyThread extends Thread {
    SharedResource resource;

    MyThread(SharedResource res, String name) {
        super(name);
        resource = res;
    }

    public void run() {
        resource.printNumbers(Thread.currentThread().getName());
    }
}

public class Main {
    public static void main(String[] args) {
        SharedResource resource = new SharedResource();

        MyThread t1 = new MyThread(resource, "Thread 1");
        MyThread t2 = new MyThread(resource, "Thread 2");

        t1.start();
        t2.start();
    }
}

Output:

Thread 1: 1
Thread 1: 2
...
Thread 2: 1
Thread 2: 2
...

Advantages of Threads

  1. Concurrency: Perform multiple tasks simultaneously.
  2. Resource Sharing: Threads share the same memory space, reducing overhead.
  3. Improved Performance: Especially in multi-core systems.

Challenges with Threads

  1. Complexity: Managing multiple threads can be tricky.
  2. Synchronization Issues: Without proper synchronization, data inconsistencies can occur.
  3. Deadlocks: Threads waiting indefinitely for resources.

Conclusion

Threads are a powerful feature in Java, enabling developers to build efficient, concurrent applications. With proper understanding and management, you can use threads to optimize performance and enhance user experiences.

Continue exploring more advanced Java tutorials on The Coding College for a deeper dive into concurrency and other programming concepts.

Leave a Comment