Java – Getting and Setting Name of Thread | Code Factory


Index Page : Link

Donate : Link

Medium Link : Link

Applications : Link

  • Every thread in Java has some name, It may be default name generated by JVM or customized name provided by programmer.
/* Returns this thread's name. */
public final String getName();

/* Changes the name of this thread to be equal to the argument name.  */
public final void setName(String name);


Thread.currentThread().getName();
Thread.currentThread().setName("Code");
package com.example.thread;

/**
 * @author code.factory
 *
 */
public class ThreadName {
	public static void main(String... args) {
		System.out.println(Thread.currentThread().getName());
		MyThread t = new MyThread();
		System.out.println(t.getName());
		Thread.currentThread().setName("Code");
		System.out.println(Thread.currentThread().getName());
	}
}

class MyThread extends Thread {
	public void run() {
		System.out.println("Child Thread");
	}
}

Output :

main
Thread-0
Code

One thought on “Java – Getting and Setting Name of Thread | Code Factory”

Leave a comment