The Futuretask class in Java

Source: Internet
Author: User
Tags throwable

I. Overview

Under Java.util.concurrent package, class declaration: Public Classfuturetask<v> extends Object implements Runnablefuture<v>

An asynchronous computation that can be canceled. This class provides a basic implementation of the pair by taking advantage of the method of starting and canceling the calculation, querying whether the calculation is completed, and getting the results of the calculation Future . The result is only available when the calculation is complete, and if the calculation is not completed, the get method is blocked. Once the calculation is complete, you cannot start or cancel the calculation again.

You can use futuretask wrappers Callable or Runnable objects. Because Futuretask implements the Runnable, the futuretask can be submitted to Executor execution.

In addition to being a standalone class, this class provides protected functionality that can be useful when creating custom task classes.


Second, the construction method

1.public FutureTask(Callable<V> callable)   创建一个 FutureTask,一旦运行就执行给定的Callable

参数:callable - 可调用的任务。

thrown: NullPointerException -if callable is null.

2、publicFutureTask(Runnable runnable,V result)   创建一个FutureTask,一旦运行就执行给定的 Runnable,并安排成功完成时 get 返回给定的结果 。

Parameters: runnable -tasks that can be run.

           result-the result to be returned upon successful completion. If you do not need a specific result, consider using the following form of construction:future<?> f = new futuretask<object> (runnable, NULL)

thrown: NullPointerException -if runnable is null.  


三、方法详细

1、Public Boolean iscancelled () returns True if it is canceled before the task is completed properly .

specified by: Future<V> in the interfaceisCancelled

return: true If the task is canceled before it is completed


2. Public Boolean IsDone () returns trueif the task is completed. This method returns truein all of these cases, possibly due to a normal termination, exception, or cancellation.

designated by: Interface Future<V> in theisDone

return: if the task is completed, returns true


3. Public Boolean Cancel (Boolean mayinterruptifrunning)

An attempt was made to cancel execution of this task. If the task is completed, canceled, or cannot be canceled for some other reason, the attempt will fail. When cancel is called, if the call succeeds and the task has not been started, the task will never run. If the task is already started, the mayinterruptifrunning parameter determines whether the thread that performs this task should be interrupted in a way that attempts to stop the task.

After this method returns, Future.isDone() subsequent calls to the pair will always return true. If this method returns True, Future.isCancelled() subsequent calls to the pair will always return true.

Designated by:
Future<V> in the interface cancel .
Parameters:
mayInterruptIfRunning- trueif the thread that performs this task should be interrupted, otherwise the running task is allowed to run complete
Return:
Returns falseif the task cannot be canceled, usually because it has completed normally; otherwise true
4, public V get () throwsinterruptedexception ,executionexception


if necessary, wait for the calculation to complete and then get its results.

Designated by:
Future<V> in the interface get .
Return:
The result of the calculation
Thrown:
-If the
CancellationException calculation is canceled
-if
InterruptedException the current thread is interrupted while waiting
ExecutionException -If the calculation throws an exception
5. PublicV Get (long timeout,  Timeunit unit) throwsInterruptedexception ,  Executionexception ,TimeoutException

If necessary, wait for the result to be obtained (if the result is available) after the time given for the calculation to complete.

Designated by:
Future<V> in the interface get .
Parameters:
timeout -Maximum time to wait
unit -the time unit of the timeout parameter
Return:
The result of the calculation
Thrown:
-If the
CancellationException calculation is canceled
-if
InterruptedException the current thread is interrupted while waiting
-if the
ExecutionException calculation throws an exception
TimeoutException -If the wait time-out
/c0>
6. protected void Done () The protected method is called when this task transitions to the state IsDone(whether normal or canceled). The default implementation does not perform any action. Subclasses can override this method to invoke completion callbacks or to perform bookkeeping. Note that you can query the state within the implementation of this method to determine whether the task has been canceled.


7. protected void Set (v ) Set the result to the given value unless the future has been set or canceled. This method is called internally by the run method when the calculation completes successfully.


8. protected void setexception (throwable t) unless this future has been set or canceled, it will report a executionexceptionand give the given throwable for its cause. This method is called internally by the run method when the calculation fails.


9. Public void Run () set it to the result of its calculation unless the future has been canceled.


10. protected Boolean Runandreset ()  

Performs a calculation without setting its results, resets the future to its original state, and fails if the calculation encounters an exception or is canceled. This operation is designed for tasks that are essentially performed several times.

Return:
returns True if the run is successful and reset.








The Futuretask class in Java

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.