Java Data Type Tutorial - Java Thread.setName(String name)








Syntax

Thread.setName(String name) has the following syntax.

public final void setName(String name)

Example

In the following code shows how to use Thread.setName(String name) method.

public class Main {
//  w w  w  . j av  a 2 s.  co  m
   public static void main(String[] args) {

     Thread t = Thread.currentThread();
     System.out.println("Thread = " + t);
    
     t.setName("java2s.com thread");
     
     System.out.println("Thread after changing name = " + t);
    
     int count = Thread.activeCount();
     System.out.println("currently active threads = " + count);
   }
}

The code above generates the following result.