Testing GetName methods and GetID methods in threads

Source: Internet
Author: User
Tags thread class custom name

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.