Java-multithreaded callable, executors, future

Source: Internet
Author: User

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

Introduction

The callable interface represents a piece of code that can be called and returns the result;

The future interface represents an asynchronous task, which is the result of a 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, which is 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 useful ways to perform tasks within a callable in a thread pool. Since the callable task is parallel, we must wait for it to return the result.

Future Object : The Java.util.concurrent.Future object solves this problem for us. 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 result of the callable return. The future provides a get () method so that we can wait for callable to end and get its execution results.

Callable the source of the interface:

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

The source of the Future interface:

    Public interface Future<v> {          boolean  cancel (boolean mayinterruptifrunning);//attempt to cancel execution of this task          Boolean  iscancelled ();      Returns True          Boolean  isDone () If the task is canceled before it is completed properly;           Returns True          V get () throws Interruptedexception, executionexception if the task is completed, and/  /If necessary, wait for the calculation to complete and get the result          // If necessary, wait for the result to be obtained (if the result is available) after the time given for the calculation to complete.          V  Get (long timeout, timeunit unit) throws Interruptedexception, Executionexception, timeoutexception;      }  


An 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 for parallel execution, the result is saved in the future object, and finally the result of the execution can be extracted with the Get () method of the future object.

2. Excutorservice interface

Executorservice provides methods for managing termination, and can be generated to track one or more asynchronous task execution conditions Future the method.

Main functions:

<T> future<t>Submit(callable<t> Task) a task that submits a return value for execution, 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 successfully completed. If you want to immediately block the wait for a task, you can use result = Exec.submit (acallable). get ();    form of construction.    Note: The executors class includes a set of methods that you can use to convert some other common closure-like objects, such as converting privilegedaction to callable form, so that you can commit them. Parameters: Task-Return of the tasks to be submitted: represents the future throw of the task waiting to complete: rejectedexecutionexception-If the task cannot be scheduled to execute Nu Llpointerexception-If the task is nullAttention: For the use and callable of the submit, refer to theusing callable to 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


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.