How do I identify a thread?
Author: Deron Eriksson
Description: This Java tutorial shows how to give a thread a name so that it can be identified.
Tutorial created using: Windows Vista || JDK 1.6.0_11 || Eclipse JEE Ganymede SR1 (Eclipse 3.4.1)


When dealing with multithreaded applications, it can be difficult to identify particular threads that are executing. One way to differentiate threads is to specify names for them. If a name isn't specified for a thread, it will be given a default name. A thread can be given a name in its constructor. In addition, it can be specified via a Thread object's setName() method. In addition, it should be noted that a thread is given an identification number that can be retrieved via the thread's getId() method. This thread doesn't change for the life of the thread, so that if a thread's name changes, it can still be identified by its Id number.

To demonstrate this, let's first create a class that implements the Runnable interface. The code in RunnableJob's run() method will be executed by a thread. We can obtain a reference to this thread via Thread.currentThread(), and we can call getName() on this reference and display the name. We can display its identification number via getId().

RunnableJob.java

package com.cakes;

public class RunnableJob implements Runnable {

	@Override
	public void run() {
		Thread thread = Thread.currentThread();
		System.out.println("RunnableJob is being run by " + thread.getName() + " (" + thread.getId() + ")");
	}

}

The ThreadExample class creates a Thread object and names it "thread1" via the thread's constructor. It starts the thread. Following this, it creates another thread and names it "thread2" via the thread's setName() method. It starts this thread. After this, it creates another thread but doesn't give it a name so that the default name will be used. This thread is also started. Following this, it gets a reference to the main thread via Thread.currentThread() and displays the name and Id number of this thread.

ThreadExample.java

package com.cakes;

public class ThreadExample {

	public static void main(String[] args) throws InterruptedException {

		RunnableJob runnableJob = new RunnableJob();

		Thread thread1 = new Thread(runnableJob);
		thread1.setName("thread1");
		thread1.start();

		Thread thread2 = new Thread(runnableJob, "thread2");
		thread2.start();

		Thread thread3 = new Thread(runnableJob);
		thread3.start();

		Thread currentThread = Thread.currentThread();
		System.out.println("Main thread: " + currentThread.getName() + "(" + currentThread.getId() + ")");

	}

}

The console output from the execution of ThreadExample is shown here.

Console Output

RunnableJob is being run by thread1 (11)
RunnableJob is being run by thread2 (12)
Main thread: main(1)
RunnableJob is being run by Thread-1 (13)

Notice that the results are displayed in an order that might not be expected. This is due to the nature of multithreaded applications. Notice that we see the names "thread1" and "thread2" that we assigned, and we also see that the default name of "Thread-1" was assigned to our third thread. Also, notice that the main thread's name is "main".