Home Tutorial Java Thread Java Thread setName

 
 

Java Thread setName
Posted on: November 7, 2009 at 12:00 AM
In this section of java tutorial we will learn about the Java Thread setName() method and its use. Then We will create an example and show its usage.

  • Java Thread setName() method sets the new name to each Thread.
  • It is used in both Thread class and Runnable interface.
  • Name is also set by the string data used in the constructor.


Java Thread setName Example

public class setname implements Runnable {
	@Override
	public void run() {
		System.out.println(Thread.currentThread());
	}

	public static void main(String[] args) {
		Thread t1 = new Thread(new setname(), "Thread a");
		t1.start();
		Thread t2 = new Thread(new setname());
		t2.setName("Thread b");
		t2.start();
		System.out.println(t1.getName());
	}
}

Output

Thread[Thread a,5,main] Thread a Thread[Thread b,5,main]

Related Tags for Java Thread setName:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.