V结果返回的类型的futuretask的
get方法
public class FutureTask<V> extends Object implements RunnableFuture<V>
Future基实现,用的方法启动和取消计算,查询是否计算完成,并检索结果。结果只能检索时,计算已完成的
get方法将阻塞;如果计算尚未完成。一旦计算完成,计算不能重新启动或取消(除非计算时使用
runAndReset())。
一个FutureTask可以用来包装Callable或Runnable对象。因为FutureTask实现Runnable,一FutureTask可以提交执行Executor。
除了作为一个独立的类,这个类提供了protected功能可能是有用的当创建自定义任务类。
| Constructor and Description |
|---|
FutureTask(Callable<V> callable)
创建一个
FutureTask会在运行,执行给定的
Callable。
|
FutureTask(Runnable runnable, V result)
创建一个
FutureTask会在运行,执行给定的
Runnable,并安排
get将给定的成功完成的结果返回。
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
cancel(boolean mayInterruptIfRunning)
试图取消此任务的执行。
|
protected void |
done()
保护方法调用时,这个任务转换到状态
isDone(是否正常或通过取消)。
|
V |
get()
等待,如果需要计算完成,然后检索其结果。
|
V |
get(long timeout, TimeUnit unit)
如果需要的话,在大多数给定的计算时间完成,然后检索其结果,如果可用。
|
boolean |
isCancelled()
返回
true如果这个任务完成之前取消正常。
|
boolean |
isDone()
返回
true如果完成这个任务。
|
void |
run()
将这个未来设置为它的计算结果,除非它已被取消。
|
protected boolean |
runAndReset()
执行计算的结果没有设置,然后重置这个未来的初始状态,而如果计算遇到异常或取消,这样做。
|
protected void |
set(V v)
将这个未来的结果设置为给定的值,除非这个未来已经被设置或已被取消。
|
protected void |
setException(Throwable t)
使这本报告
ExecutionException与给定的时间作为它的原因,除非这个未来已经设置或取消了。
|
public FutureTask(Callable<V> callable)
FutureTask会在运行,执行给定的
Callable。
callable - Callable任务
NullPointerException如果赎回是空的
public FutureTask(Runnable runnable, V result)
FutureTask会在运行,执行给定的
Runnable,并安排
get将给定的成功完成的结果返回。
runnable的Runnable任务
result -结果成功完成返回。如果你不需要一个特定的结果,考虑使用的形式结构:
Future<?> f = new FutureTask<Void>(runnable, null)
NullPointerException如果Runnable是空的
public boolean isCancelled()
Future
true如果这个任务完成之前取消正常。
isCancelled 接口
Future<V>
true如果这个任务完成之前取消
public boolean isDone()
Future
true如果完成这个任务。完成可能是由于正常终止,例外,或取消——在所有这些情况下,此方法将返回
true。
public boolean cancel(boolean mayInterruptIfRunning)
Future
cancel称,这个任务不应该跑。如果任务已经开始,然后
mayInterruptIfRunning参数确定线程执行这个任务应该是为了阻止任务中断。
此方法返回后,随后调用Future.isDone()将总是返回true。随后调用Future.isCancelled()永远如果这个方法返回的返回true true。
public V get() throws InterruptedException, ExecutionException
Future
get 接口
Future<V>
CancellationException如果计算被取消
InterruptedException -如果当前线程被中断等待
ExecutionException如果计算抛出一个异常
public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
Future
get 接口
Future<V>
timeout -最大等待时间
unit - timeout参数的时间单位
CancellationException如果计算被取消
InterruptedException -如果当前线程被中断等待
ExecutionException如果计算抛出一个异常
TimeoutException如果等待超时
protected void done()
isDone(是否正常或通过取消)。默认实现不做任何事。子类可以重写此方法调用完成回调或执行簿记。请注意,您可以在该方法的实现内查询状态,以确定此任务是否已被取消。
protected void setException(Throwable t)
ExecutionException与给定的时间作为它的原因,除非这个未来已经设置或取消了。
这个方法调用内部的run()方法在计算失败。
t -失败的原因
public void run()
RunnableFuture
run 接口
Runnable
run 接口
RunnableFuture<V>
Thread.run()
protected boolean runAndReset()
true如果成功运行和复位
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.