The difference between Thread and Runnable in Java: threadrunnable

Source: Internet
Author: User

The difference between Thread and Runnable in Java: threadrunnable

Turn: http://blog.csdn.net/wwww1988600/article/details/7309070

In java, You can implement multithreading in two ways. One is to inherit the Thread class, the other is to implement the Runnable interface, and the Thread class is defined in the java. lang package. As long as a class inherits the Thread class and overwrites the run () method in this class, it can implement multi-threaded operations. However, a class can only inherit one parent class, which is the limitation of this method.

Differences and links between the two implementation methods:

In program development, as long as it is multi-thread, it will always be dominated by the implementation of the Runnable interface, because the implementation of the Runnable interface is

Inheriting the Thread class has the following benefits:

-> Avoid the limitations of point inheritance. A class can inherit multiple interfaces.

-> Suitable for Resource Sharing (this is incorrect)

Explanation: in java, to share resources, you must use the Semaphore mechanism. There are two methods: 1. Use Semaphore 2 and use Synchronize.

The specific example will not be moved.

Related Article

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.