Test the GetName method of the thread class and the method of Geti, 1. Create two threads, output the default thread name and the default ID. 2. Create a thread, set the name of the thread and lose the outgoing name and default ID.
To get the default thread name and ID, first, new class Testthreadnameandid, in the main method of the class, create two threads t0, T1, and then use the thread class's GetName method and GetID method to get the name and ID of the threads.
Get the thread name and ID
public class Testthreadnameandid {
GetName method and GetID method for testing thread
public static void Main (string[] args) {
Thread t = new thread ();
System.out.println (T.getname ());
System.out.println (T.getid ());
thread T1 = new Thread ();
System.out.println (T1.getname ());
System.out.println (T1.getid ());
}
}
As can be seen from the output, the default thread name is "thread-+ number", the ID is starting with the number 9, because the number before 9 is consumed by the thread of the virtual machine.
To add a custom name to a thread, when constructing an object of the thread class, you can use the thread (String) constructor to customize the name.
public class Testthreadnameandid {
GetName method and GetID method for testing thread
public static void Main (string[] args) {
Thread t = new thread ();
System.out.println (T.getname ());
System.out.println (T.getid ());
thread T1 = new Thread ();
System.out.println (T1.getname ());
System.out.println (T1.getid ());
Thread t2 = new Thread ("Thread of the custom name");
System.out.println (T2.getname ());
System.out.println (T2.getid ());
}
}
Console output:
Testing GetName methods and GetID methods in threads