Two different types of multithreading

Source: Internet
Author: User

See the difference between the source code:

* Inherit thread: Because the subclass overrides the thread class's run (), when Start () is called, the Run () method of the subclass is searched directly

* Implement runnable: A reference to the runnable is passed in the constructor, the member variable remembers it, and start () calls the run () method internally to determine if the reference to the member variable runnable is empty, and does not empty compile-time view of the runnable run (), Run-time execution is a subclass of the run () method

Inherit thread

* Advantage: You can use the method in the thread class directly, the code is simple

* The disadvantage is that if you already have a parent class, you can't use this method

Implementing the Runnable Interface

* The advantage is that even if the thread class you define has a parent class, it doesn't matter because the parent class can implement the interface, and the interface can be implemented more

* The disadvantage is: you cannot directly use the method in thread, you need to get to the thread object before you can get thread method, code complex

Two different types of multithreading

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.