Example: Setting a thread attribute in a Java program

This example shows how to set the name thread attribute in a Java™ program.

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.
/*
FileName: ATEST10.java
 
The output of this example is as follows:
Entered the testcase
Create a thread
Set some thread attributes
Start the thread
Wait for the thread to complete
Entered the thread: "theThread"
Testcase completed
*/
 
import java.lang.*;
 
public class ATEST10 {
 
   static class theThread extends Thread {
      public void run() {
         System.out.print("Entered the thread: \"" + getName() +
                          "\"\n");
      }
   }
 
   public static void main(String argv[]) {
      System.out.print("Entered the testcase\n");
 
      System.out.print("Create a thread\n");
      theThread t = new theThread();
 
      System.out.print("Set some thread attributes\n");
      t.setName("theThread");
 
      System.out.print("Start the thread\n");
      t.start();
 
      System.out.print("Wait for the thread to complete\n");
      try {
         t.join();
      }
      catch (InterruptedException e) {
         System.out.print("Join interrupted\n");
      }
 
      System.out.print("Testcase completed\nj");
      System.exit(0);
   }
 
}