How do I check if a thread is alive?

A thread is alive or runningn if it has been started and has not yet died. To check whether a thread is alive use the isAlive() method of Thread class. It will return true if this thread is alive, otherwise return false.

package org.kodejava.example.lang;

public class ThreadAliveDemo implements Runnable {

    public void run() {
        System.out.println("Running [" +
            Thread.currentThread().getName() + "].");

    public static void main(String[] args) {
        Thread t1 = new Thread(new ThreadAliveDemo(), "FirstThread");
        Thread t2 = new Thread(new ThreadAliveDemo(), "SecondThread");

        // start the t1

        // Check to see if the first thread is alive or not.
        if (t1.isAlive()) {
            System.out.format("%s is alive.%n", t1.getName());
        } else {
            System.out.format("%s is not alive.%n", t1.getName());

        // Check to see if the second thread is alive or not.
        // It should be return false because t2 hasn't been started.
        if (t2.isAlive()) {
            System.out.format("%s is alive.%n", t2.getName());
        } else {
            System.out.format("%s is not alive.%n", t2.getName());

Wayan Saryada

Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. You can support my works by donating here. Thank you 🥳

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.