Multithreading-----The difference between the thread class and the Runnable interface

Source: Internet
Author: User
Tags thread class

The first inherits the thread class to realize multi-threading, actually is equivalent to take out three things namely three sells the breakfast 10 parts the task separately to three the window, they each do each thing each sells each breakfast each completes each task, because Mythread inherits the Thread class, So in the newmythread when the creation of three objects at the same time created three threads, the implementation of runnable, equivalent to take out a 10 servings of the task for three people to work together to complete, newmythread equivalent to create a task, and then instantiate three thread, Create three threads to schedule three windows to execute.

A class can inherit only one parent class, there are limitations, and one class may implement multiple interfaces. runnable interface call thread thread (Runnable run) or thread (runnablerun,string name) construction method when creating a process, use the same runnable instance, the established multi-threaded instance variable is also shared; but by inheriting threadrunnable interface suitable for resource sharing; of course, inheritance threadthreadstatic variable;

In fact, abstract, this is not the difference between the Thread class and the Runnable interface, which can be seen as an interface and inheritance problems. We understand the interface and inheritance, it is not difficult to understand the Thread and Runnable.

it could be a little confusing at first touch. This is different from the connection, but after practice and summary we will find that this is two completely different implementations of multi-threading, one is multiple threads to complete their own tasks, one is multiple threads together to complete a task. In fact, in the implementation of a task with a number of threads to do can also be used to inherit the thread class to achieve, but rather troublesome, generally we use the implementation of the Runnable interface to achieve.

Reference: http://blog.csdn.net/xdd19910505/article/details/50732328

If a class inherits the thread, it is not suitable for resource sharing. However, if the Runable interface is implemented, it is easy to realize the resource sharing.

The benefits of implementing the Runnable interface are more than inheriting the thread class:

1): Suitable for multiple threads of the same program code to process the same resource

2): Can avoid the restriction of single inheritance in Java

3): Increase the robustness of the program, the code can be shared by multiple threads, code and data Independent

Multithreading-----The difference between the thread class and the Runnable interface

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.