Java-multithreaded callable, executors, future

Source: Internet
Author: User

http://blog.csdn.net/pipisorry/article/details/44341579

Introduction

The callable interface represents a section of code that can invoke and return results;

The future interface represents an asynchronous task. Is the future outcome of the task that has not yet been completed.

So callable is used to produce results, and the future is used to get results.


Callable Interface : Java 5 introduces the Java.util.concurrent.Callable interface in the concurrency package. It is very similar to the Runnable interface, but it can return an object or throw an exception. The callable interface uses generics to define its return type.

Executors class : provides some practical ways to run tasks within a callable in a thread pool. Because the callable task is parallel, we must wait for it to return the result.

Future Object : Java.util.concurrent.Future object for us to overcome this problem. After the online pool submits the callable task, it returns a future object that can be used to know the status of the callable task and get the results of the callable returned. The future provides a get () method that allows us to wait for callable to end and get its running results.

Source code for the callable interface:

    Public interface Callable<v> {          V call () throws Exception;//calculation result      }  

Source code for the Future interface:

    Public interface Future<v> {          boolean  cancel (boolean mayinterruptifrunning);//attempt to cancel the run of this task          Boolean  iscancelled ();      If you cancel the task before it is finished, it returns True          Boolean  isDone ();           If the task is complete, return true          V  get () throws Interruptedexception, executionexception;//if necessary. Wait for the calculation to complete and then get its result          //If necessary, wait up to the time given for the calculation to get its result (assuming the result is available).          V  Get (long timeout, timeunit unit) throws Interruptedexception, Executionexception, timeoutexception;      }  


Example

Private Executorservice executor = Executors.newfixedthreadpool (numberofthreads);
Try {callable<topicmodel> callable = new topicmodelcallable (Corpus,param); future<topicmodel> future = Executor.submit (callable); Futurelist.add (the Future);} catch (Exception ex) {ex.printstacktrace ();}

for (future<topicmodel> future:futurelist) {Topicmodel Topicmodel = Future.get (); Topicmodellist.add (TopicModel );}
Note:

1. Create callable object callable with corpus and corresponding parameters, submit to executor to run in parallel, the result is saved in the future object. Finally, the result can be extracted using the Get () method of the future object.

2. Excutorservice interface

Executorservice provides methods for managing termination, and can be generated to track the health of one or more asynchronous tasks Future the method.

Main functions:

<T> future<t>Submit(callable<t> Task) a task that submits a return value is used to run, returning a future that represents the pending result of the task.

The Get method for the future returns the result of the task when it is completed successfully. If you want to block the wait for a task immediately, you can use result = Exec.submit (acallable). get (); form of construction.

Note: The executors class contains a set of methods that can convert some of the other common closure-like objects, such as converting privilegedaction to callable form, so that they can be submitted. Number of parameters: task-Return of the tasks to be submitted: Indicates that the task waits for the future to be thrown: rejectedexecutionexception-assuming the task cannot be scheduled to run Nu Llpointerexception-Assume that the task is null Note: The use of the submit and callable can refer to the use of callable return results

[Introduction to Executor, Executorservice, threadpoolexecutor in Java]

from:http://blog.csdn.net/pipisorry/article/details/44341579

ref:http://blog.csdn.net/mazhimazh/article/details/19291965

[How to manage the Java thread pool and build a distributed Hadoop scheduling framework]


Java-multithreaded callable, executors, future

Related Article

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.