Java Basics-A comparison of three ways to create threads

Source: Internet
Author: User

The previous blog introduced three ways to create threads, so what are the different places and the same places in the three ways? Today we'll talk a little bit about the difference between these three ways.

First, multiple threads can be implemented by inheriting the thread class or implementing runnable, callable interfaces, but implementing the Runnable interface is essentially the same as implementing the callable interface, except that the method defined in the callable interface has a return value. You can declare an exception to be thrown. Therefore, the implementation of runnable interface and implementation of the callable interface can be classified as a way. The main differences between this approach and the inheritance thread approach are as follows:

1 advantages and disadvantages of creating multithreading by implementing runnable and callable interfaces

The thread class only implements the Runnable interface or the callable interface, and can also inherit other classes.

In this way, multiple threads can share the same target object, so it is ideal for multiple threads of the same thread to handle the same resource's application scenario, thus separating the CPU, code, and data to form a clear model that better embodies the object-oriented idea.

The bad part: programming is slightly more complex, and if you need to access the current thread, you must use the Thread.CurrentThread () method.

2 advantages and disadvantages of creating multithreading in a way that inherits the thread class:

The advantage is that writing is simple, if you need to access the current thread, you do not need to use the Thread.CurrentThread () method to get the current thread using this directly.

The disadvantage is that because the thread class has inherited the thread class, it is no longer possible to inherit other parent classes, which cannot be extended and enhanced. In general, multithreading program development is commonly used to implement runnable and callable interfaces.

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.