Course Content
Core Java
About Lesson

The Thread class in Java provides several methods to manage and control threads. These methods facilitate tasks such as starting and stopping threads, checking thread status, setting thread properties, and handling thread interruptions.

Common Methods

start()

Starts the execution of the thread. The run() method of the thread will be invoked by the JVM when the thread is scheduled for execution.

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

    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start(); // Starts the thread, executes MyThread's run() method
    }
}

run()

Contains the code that constitutes the task or behavior of the thread. This method needs to be overridden in a subclass of Thread or passed as a Runnable to the Thread constructor. If this thread was constructed using a separate Runnable run object, then that Runnable object’s run method is called; otherwise, this method does nothing and returns.

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

    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start(); // Starts the thread, executes MyThread's run() method
    }
}

join()

Waits for the thread to die. It causes the current thread to block until the thread on which join() is called completes its execution.

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

    public static void main(String[] args) throws InterruptedException {
        MyThread thread = new MyThread();
        thread.start(); // Starts the thread
        thread.join(); // Waits for the thread to complete
        System.out.println("Thread has completed");
    }
}

sleep(long millis)

Causes the thread to temporarily pause execution for the specified number of milliseconds.

public class Main {
    public static void main(String[] args) {
        System.out.println("Thread will sleep for 3 seconds");
        try {
            Thread.sleep(3000); // Sleep for 3 seconds
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Thread woke up");
    }
}

interrupt()

Interrupts the thread, causing it to throw an InterruptedException. It is typically used to terminate the thread gracefully or to wake up a sleeping thread.

public class MyThread extends Thread {
    public void run() {
        try {
            Thread.sleep(5000); // Sleep for 5 seconds
            System.out.println("Thread woke up");
        } catch (InterruptedException e) {
            System.out.println("Thread interrupted");
        }
    }

    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start(); // Starts the thread
        thread.interrupt(); // Interrupts the thread
    }
}

isAlive()

Checks whether the thread is alive. A thread is considered alive from the moment start() is called until the thread dies naturally or by explicit termination.

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

    public static void main(String[] args) throws InterruptedException {
        MyThread thread = new MyThread();
        System.out.println("Before starting thread: " + thread.isAlive()); // false
        thread.start(); // Starts the thread
        System.out.println("After starting thread: " + thread.isAlive()); // true
        thread.join(); // Waits for the thread to complete
        System.out.println("After thread completes: " + thread.isAlive()); // false
    }
}

setName(String name) and getName()

setName() sets the name of the thread, and getName() retrieves the name of the thread.

public class Main {
    public static void main(String[] args) {
        Thread thread = new Thread();
        thread.setName("MyThread");
        System.out.println("Thread Name: " + thread.getName()); // MyThread
    }
}

setPriority(int priority) and getPriority()

setPriority() sets the priority of the thread, and getPriority() retrieves the priority of the thread. Priorities range from Thread.MIN_PRIORITY (1) to Thread.MAX_PRIORITY (10), with Thread.NORM_PRIORITY (5) as the default.

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

    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.setPriority(Thread.MAX_PRIORITY); // Set max priority
        thread.start(); // Starts the thread
    }
}

Scroll to Top