Thread class constructors

  1. Thread t = new Thread();
  2. Thread t = new Thread(Runnable r);
  3. Thread t = new Thread(String name);
  4. Thread t = new Thread(Runnable r, String name);
  5. Thread t = new Thread(ThreadGroup g, String name);
  6. Thread t = new Thread(ThreadGroup g, Runnable r);
  7. Thread t = new Thread(ThreadGroup g, Runnable r, String name);
  8. Thread t = new Thread(ThreadGroup g, Runnable r, String name, long stacksize);

Setting & Getting the name of Thread

Thread class defines the following methods to set and get the name of a Thread.

  1. public final void setName(String name);
  2. public final String getName();

Example:

public class Test {

	public static void main(String[] args) {
		
	     System.out.println(Thread.currentThread().getName());
	     Thread.currentThread().setName("java2learn");
	     System.out.println(Thread.currentThread().getName());
	}

}
/*
Output:
main
java2learn
*/