Future Mode
The future interface is the implementation of the Java threading future pattern, which can be used for asynchronous computation
Callable is responsible for producing results, future is responsible for taking results
This can be explained as:
I have a task, submitted to the future, the future will inform callable for me to complete this task, during which I can do anything I want to do. After a while, the results can be taken from the future.
callable and future interfaces
Callable interface
The callable interface is similar to runnable, but Runnable does not return a result, and callable can return the result, which can be obtained by the future, that is, the future can get the return value of the asynchronous execution task
/** * Computes a result, or throws an exception if unable to does so. * @return computed result @throws Exception If unable to compute a result< c10/>* /throws
Future interface
The future interface has the following methods:
1. Cancel the execution of the task. parameter specifies whether the task execution is interrupted immediately, or the end of the task
boolean Cancel (boolean mayinterruptifrunning)
2. If the task has been canceled and the task is canceled before it is completed correctly, returns True
Boolean
3. Whether the task has been completed. It is important to note that if a task terminates normally, an exception, or a cancellation, it will return true
Boolean
4. Wait for the task to finish, and then get the result of type V. Interruptedexception thread is interrupted exception, executionexception task execution exception, if the task is canceled, will also throw cancellationexception
V get ()
5. As with the Get function above, set the time-out. The parameter timeout specifies the time-out period, the units of the uint specified times, and the associated definitions in the enumeration class Timeunit. If the calculation times out, TimeoutException will be thrown
V get (long
The future interface provides a way to detect whether a task has been executed, waiting for the task to finish executing the results. You can also set the time-out for task execution, which is the key to implementing a Java program timeout.
Therefore, if you need to set the maximum time code execution, that is, time-out, you can use the Java thread pool Executorservice class with the future interface to achieve.
int result = Future.get (timeunit.milliseconds);
Here are some basic ways to use callable and future:
1 Public classTest {2 3 Public Static voidMain (string[] args) {4Executorservice ThreadPool =executors.newsinglethreadexecutor ();5future<string> future =6 Threadpool.submit (7 NewCallable<string>(){8 PublicString Call ()throwsException {9Thread.Sleep (2000);Ten return"Shen_smile"; One } A }); -System.out.println ("Wait for results"); - Try { theSystem.out.println ("Get Results" +future.get ()); -}Catch(Interruptedexception |executionexception e) { - //TODO auto-generated Catch block - e.printstacktrace (); + } - } +}
This article refers to some of the content in http://blog.csdn.net/vking_wang/article/details/9470499
Callable and future