The difference between thread and runnable in Java

Source: Internet
Author: User
Tags semaphore

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

There are two ways to implement multithreading in Java, one is to inherit the thread class, one to implement the Runnable interface, and the thread class to be defined in the Java.lang package. A class that inherits the thread class to overwrite the run () method in this class can implement multithreaded operations, but only one parent class can inherit from a class, which is the limitation of this method.

The difference between the two ways of implementation and contact:

In the program development, as long as the multithreading is always to achieve runnable interface-based, because the implementation of runnable interface compared to

Inheriting the thread class has the following benefits:

To avoid the limitations of point inheritance, a class can inherit multiple interfaces.

--Suitable for resource sharing ( this is wrong )

explanation: In Java, in order to realize the sharing of resources, we need to use the semaphore mechanism, there are two ways: 1, using semaphore 2, using Synchronize

The concrete example will not move.

Differences between thread and runnable in Java

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.