How do I set and get the name of a thread?

You can assign a name to thread instance by using the setName() method and get the name of the thread using the getName() method. The naming support is also available as a constructor of the Thread class such as Thread(String name) and Thread(Runnable target, String name).

package org.kodejava.lang;

public class ThreadNameDemo extends Thread {
    public ThreadNameDemo() {
    }

    public ThreadNameDemo(String name) {
        super(name);
    }

    public static void main(String[] args) {
        Thread thread1 = new ThreadNameDemo();
        thread1.setName("FOX");
        thread1.start();

        Thread thread2 = new ThreadNameDemo("DOG");
        thread2.start();
    }

    @Override
    public void run() {
        // Call getName() method to get the thread name of this
        // thread object.
        System.out.println("Running [" + this.getName() + "]");
    }
}

How do I check if a thread is alive?

A thread is alive or running 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.lang;

public class ThreadAliveDemo implements Runnable {

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

        // start the t1
        t1.start();

        // 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());
        }
    }

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

How do I check if a thread is a daemon thread?

You can test if a thread is a daemon thread or a user thread by calling the isDaemon() method of the Thread class. If it returns true then the thread is a daemon thread, otherwise it is a user thread.

package org.kodejava.lang;

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

        t1.setDaemon(true);
        t1.start();
        t2.start();

        if (t1.isDaemon()) {
            System.out.format("%s is a daemon thread.%n", t1.getName());
        } else {
            System.out.format("%s is a user thread.%n", t1.getName());
        }

        if (t2.isDaemon()) {
            System.out.format("%s is a daemon thread %n", t2.getName());
        } else {
            System.out.format("%s is a user thread %n", t2.getName());
        }
    }

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

The code snippet print the following output:

FirstThread is a daemon thread.
SecondThread is a user thread 
Running [FirstThread]
Running [SecondThread]

How do I create a daemon thread?

A daemon thread is simply a background thread that is subordinate to the thread that creates it, so when the thread that created the daemon thread ends, the daemon thread dies with it. A thread that is not a daemon thread is called a user thread. To create a daemon thread, call setDaemon() method with a true boolean value as argument before the thread is started.

package org.kodejava.lang;

public class DaemonThread implements Runnable {
    private final String threadName;

    private DaemonThread(String threadName) {
        this.threadName = threadName;
    }

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

        // t1 is as daemon thread
        t1.setDaemon(true);
        t1.start();

        // t2 is a user thread
        t2.start();
    }

    public void run() {
        System.out.println("Running [" + threadName + "]");
    }
}

How do I use the sleep method of the Thread class?

Here is another example that use the Thread.sleep() method. In the example we create two instances of the ThreadSleepAnotherDemo, we give each thread a name and the sleep interval so that we can see how to thread execution.

package org.kodejava.lang;

import java.util.Calendar;

public class ThreadSleep implements Runnable {
    private final String threadName;
    private final long sleep;

    public ThreadSleep(String threadName, long sleep) {
        this.threadName = threadName;
        this.sleep = sleep;
    }

    public static void main(String[] args) {
        Thread thread1 = new Thread(new ThreadSleep("FirstThread", 1000));
        Thread thread2 = new Thread(new ThreadSleep("SecondThread", 3000));

        // Start the threads
        thread1.start();
        thread2.start();
    }

    // The run() method will be invoked when the thread is started.
    public void run() {
        System.out.println("Start thread [" + this.threadName + "]");
        try {
            while (true) {
                // Pause the thread for "sleep" milliseconds.
                Thread.sleep(this.sleep);
                System.out.println("[" + threadName + "]" +
                        Calendar.getInstance().getTime());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Finish thread [" + this.threadName + "]");
    }
}

These are the sample output produced by the code snippet above:

Start thread [FirstThread]
Start thread [SecondThread]
[FirstThread]Mon Oct 25 11:52:13 CST 2021
[FirstThread]Mon Oct 25 11:52:14 CST 2021
[SecondThread]Mon Oct 25 11:52:15 CST 2021
[FirstThread]Mon Oct 25 11:52:15 CST 2021
[FirstThread]Mon Oct 25 11:52:16 CST 2021