3 Ways to implement multithreading

Source: Internet
Author: User
Tags instance method thread class

1. Inherit the thread class, overload the Run method

2. Implementing the Runnable interface, overloading the Run method

3. Use callable, with the future, and Executorservice to achieve multi-threaded return results

The first two methods are non-return values, the essence of the thread class is also implemented Runnable interface, representing a thread of the instance, through the thread class's start instance method (is the native method), will create a new thread, and execute the thread of the Run method

The way to implement the Runnable interface is more flexible, and if the class has inherited another class, it can only be implemented by inheriting runnable.

JDK1.5 introduced a new callable with return value, supporting the Executorservice thread pool interface and the future to use, can achieve a multi-threaded return value, Executorservice interface provides a Submit method, passing a callable or R Unnable, return to the future, if the executor background thread pool has no computed callable, then the call to the future object get () method will block to the completion of the calculation

3 Ways to implement 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.