Two ways to create a thread

Source: Internet
Author: User
The first kind: Inherit the thread class subclass to overwrite the Run method of the parent class, and write the threading code in Run (). A subclass object is created, and the thread is also built. Start the thread by using the Start method. The second is to implement the Run method of the Runnable interface Subclass Overlay interface and write the thread code in run (). Creates a thread from the thread class and passes the subclass object implementing the Runnable interface as a parameter to the constructor of the thread class. The thread object calls the Start method to start the threads. The comparison between the two: inherit the thread class, relatively simple, but because of the limitations of single inheritance, the same resources are not shared, but not recommended to use, and implement the Runnable interface, multiple threads share a resource, the class can inherit other classes, implement multiple interfaces, so it is recommended to use.

Two ways to create a thread

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.