Java Thread Method

The Thread class in Java provides a variety of methods to manage and manipulate threads. These methods control the lifecycle of a thread, allow synchronization, and handle various thread-related tasks. Here’s a comprehensive list of important methods in the Thread class along with explanations and examples where necessary.

start() Method

This method is used to start the execution of a thread. When invoked, it calls the thread’s run() method internally in a new thread of execution. The run() method contains the code that will be executed by the thread.

Syntax:


public void start();

Example:


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(); // This invokes the run() method in a new thread
    }
}

Output:

Thread is running

run() Method

The run() method contains the code to be executed by the thread. This method is called by the start() method, but you need to override this method in a subclass of Thread (or in a Runnable implementation) to define the thread’s task.

Syntax:


public void run();

Example:


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

sleep(long millis) Method

The sleep() method is used to pause the execution of the current thread for the specified number of milliseconds (or microseconds, in the overloaded version). It can be interrupted, causing an InterruptedException.

Syntax:


public static void sleep(long millis) throws InterruptedException;

Example:


//ThreadExample.java file
class MyThread extends Thread {
    public void run() {
        try {
            System.out.println("Thread is going to sleep");
            Thread.sleep(1000);  // Sleep for 1 second
            System.out.println("Thread woke up");
        } catch (InterruptedException e) {
            System.out.println(e);
        }
    }
}

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

Output:

Thread is going to sleep
Thread woke up

join() Method

The join() method allows one thread to wait for the completion of another thread. When you call join() on a thread, the current thread is paused until the thread on which join() was called finishes executing.

Syntax:


public final void join() throws InterruptedException;
public final void join(long millis) throws InterruptedException;

Example:


//Main.java file
class MyThread extends Thread {
    public void run() {
        try {
            for (int i = 0; i < 5; i++) {
                System.out.println(i);
                Thread.sleep(500);
            }
        } catch (InterruptedException e) {
            System.out.println(e);
        }
    }
}

public class Main {
    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start();
        try {
            thread.join();  // Main thread waits for the completion of MyThread
        } catch (InterruptedException e) {
            System.out.println(e);
        }
        System.out.println("Thread completed, main thread resumes");
    }
}

Output:

0
1
2
3
4
Thread completed, main thread resumes

interrupt() Method

The interrupt() method is used to interrupt a thread that is either running or waiting/sleeping. This doesn't terminate the thread but sets its interrupt flag. If the thread is in a blocked or sleeping state, it will throw an InterruptedException.

Syntax:


public void interrupt();

Example:


//Main.java
class MyThread extends Thread {
    public void run() {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            System.out.println("Thread interrupted");
        }
    }
}

public class Main {
    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start();
        try {
            Thread.sleep(1000);  // Let the thread run for a while
            thread.interrupt();  // Interrupt the thread
        } catch (InterruptedException e) {
            System.out.println(e);
        }
    }
}

Output:

Thread interrupted

isAlive() Method

The isAlive() method checks whether the thread is still alive (i.e., it has been started and has not yet finished executing).

Syntax:


public boolean isAlive();

Example:


//Main.java
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();
        System.out.println("Is thread alive? " + thread.isAlive());
    }
}

Output:

Thread is running
Is thread alive? true

setName(String name) Method

The setName() method is used to set the name of the thread. If no name is provided, the thread is assigned a default name (e.g., "Thread-0", "Thread-1").

Syntax:


public final void setName(String name);

Example:


//Main.java file
class MyThread extends Thread {
    public void run() {
        System.out.println("Thread name: " + getName());
    }
}

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

Output:

Thread name: MyThread

getName() Method

The getName() method returns the name of the thread.

Syntax:


public String getName();

Example:


//Main.java file
class MyThread extends Thread {
    public void run() {
        System.out.println("Thread name: " + getName());
    }
}

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

Output:

Thread name: Thread-0

getPriority() Method

The getPriority() method returns the current priority of the thread.

Syntax:


public int getPriority();

Example:


//Main.java file
class MyThread extends Thread {
    public void run() {
        System.out.println("Thread priority: " + getPriority());
    }
}

public class Main {
    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.setPriority(Thread.MIN_PRIORITY);
        thread.start();
    }
}

Output:

Thread priority: 1

yield() Method

The yield() method is a hint to the thread scheduler that the current thread is willing to yield its current use of the processor. The thread does not necessarily stop, but it gives other threads a chance to execute.

Syntax:


public static void yield();

Example:


//Main.java file
class MyThread extends Thread {
    public void run() {
        System.out.println("Thread " + getName() + " is running");
        Thread.yield();  // Yield control to other threads
    }
}

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

Output:

Thread Thread-0 is running

currentThread() Method

The currentThread() method returns a reference to the currently executing thread.

Syntax:


public static Thread currentThread();

Example:


public class Main {
     public static void main(String[] args) {
        Thread currentThread = Thread.currentThread();
        System.out.println("Current thread: " + currentThread.getName());
    }
}

Output:

Current thread: main