Day13 multi-thread creation method, day13 multi-thread Creation

Source: Internet
Author: User

Day13 multi-thread creation method, day13 multi-thread Creation

#01 create multiple threads
Inherit Thread class
Overwrite the run method: run the code to be executed in the run method.
Create object
Call the start method. The start method starts the thread and then calls the run method.


Get the Thread name: getName () method, in the Thread class
The Thread class obtains the reference of the object in the executing Thread. The static Thread currentThread () method returns a Thread object. This object can obtain the name by calling the method to obtain the Thread name.

Thread. currentThread (). getName () Get the Thread name

 

#02
The second way to create a thread to implement the runnable interface
1. Define a class to implement the runnable interface
2. overwrite the run method of the runnable interface.
3. Create an object that inherits the runnable interface, and then use this object as a parameter to create a Thread object.
4. Call the start method of the thread object to enable the thread.

The second method is generally used.

 

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.