How to Set Thread Name in Java


In this example we will show the method to set name of thread in Java.

Source Code

package com.beginner.examples;

public class SetThreadNameExample {

    public static void main(String[] args){
    	//get current thread
    	Thread thread = Thread.currentThread();
		System.out.println(thread);
		//set thread name
		thread.setName("example");
		System.out.println(thread);
	}
}

Output:

Thread[main,5,main]
Thread[example,5,main]
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments