Detailed usage of callable, runnable, future, and executor

Source: Internet
Author: User

1. runnable interface

A class that implements the runnable interface must use an instance of the thread class to create a thread (in fact, the thread class also implements the runnable interface). It allows you to define a task that can be completed by a thread. As most people know, it only provides a run method, which neither accepts any parameters nor returns any value. If you need to return a value from an unfinished task, you must use a method outside of this interface to wait for the task to complete a message. For example:

 Runnable runnable = ...;    Thread t = new Thread(runnable);    t.start();    t.join();    String value = someMethodtoGetSavedValue()

Strictly speaking, the above Code is actually correct, but since java5.0, The callable interface has been introduced.

2. cabllable Interface

The callable interface is similar to the runnable interface, except that the runnable interface implements the run method, while the callable interface implements the call method. With reference to the callable API, this method can return an object or any specific type of object defined in generic format. Because you cannot upload the callable object to the thread object for execution, you can use the executorservice object to execute the callable object. The Service accepts the callable object and executes it through the submit method. When you submit a callable object to executorservice, a future object is returned. Then, the future get method will be blocked until the task is completed. The only requirement of the call method is that this value should be returned at the end of the call method. When the get method of future is called later, if the task runs fast, future will automatically get this value, otherwise it will wait until the value is generated. For more information:

3. Future Interface

First, understand the future mode: I have a task submitted to future, and future completes this task for me. During this period, I can do anything I want. After a while, I can retrieve the results from future. It is equivalent to a order form. After a period of time, you can take the order to pick up the goods. During this period, you can do anything else. The future interface is the order form, and the executor class is used to process the order. It produces the product according to the requirements of the future interface. The future interface is the implementation of the Java thread future mode and can be used for asynchronous computing.

4. executor Interface

The executor interface executes the submitted runnables task object. This interface provides a way to separate job submission from how each job runs (including details about thread usage and scheduling. Generally
ExecutorInstead of explicitly creating a thread. Into cute (runnalbe ). Executor uses an internal thread pool to complete operations during execution.

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.