[英]What would you use a “Future” for in App Engine?
In the docs , a FutureWrapper is defined like this: 在文档中 ,FutureWrapper的定义如下:
FutureWrapper is a simple Future that wraps a parent Future.
FutureWrapper是一个包含父Future的简单Future。
What's a Future, why would you need to wrap it and when would you use it in App Engine? 什么是未来,为什么需要包装它以及何时在App Engine中使用它?
It's the java.util.concurrent.Future<V>
. 这是
java.util.concurrent.Future<V>
。 The linked Javadoc is pretty clear and contains an example. 链接的Javadoc非常清晰,并包含一个示例。 For the lazy, here's a copypaste:
对于懒惰,这里是一个copypaste:
A
Future
represents the result of an asynchronous computation.Future
表示异步计算的结果。 Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation.提供方法以检查计算是否完成,等待其完成,以及检索计算结果。 The result can only be retrieved using method
get
when the computation has completed, blocking if necessary until it is ready.只有在计算完成时才能使用方法
get
检索结果,必要时阻塞直到准备就绪。 Cancellation is performed by the cancel method.取消由取消方法执行。 Additional methods are provided to determine if the task completed normally or was cancelled.
提供了其他方法来确定任务是否正常完成或被取消。 Once a computation has completed, the computation cannot be cancelled.
计算完成后,无法取消计算。 If you would like to use a
Future
for the sake of cancellability but not provide a usable result, you can declare types of the formFuture<?>
and returnnull
as a result of the underlying task.如果您希望为了取消可用性而使用
Future
但不提供可用的结果,则可以声明Future<?>
形式的类型,并作为基础任务的结果返回null
。Sample Usage (Note that the following classes are all made-up.)
示例用法 (请注意,以下类都已组成。)
interface ArchiveSearcher { String search(String target); } class App { ExecutorService executor = ... ArchiveSearcher searcher = ... void showSearch(final String target) throws InterruptedException { Future<String> future = executor.submit(new Callable<String>() { public String call() { return searcher.search(target); }}); displayOtherThings(); // do other things while searching try { displayText(future.get()); // use future } catch (ExecutionException ex) { cleanup(); return; } } }
The
FutureTask
class is an implementation ofFuture
that implementsRunnable
, and so may be executed by anExecutor
.FutureTask
类是Future
一个实现,它实现了Runnable
,因此可以由Executor
。 For example, the above construction with submit could be replaced by:例如,上述带提交的结构可以替换为:
FutureTask<String> future = new FutureTask<String>(new Callable<String>() { public String call() { return searcher.search(target); }}); executor.execute(future);
Memory consistency effects: Actions taken by the asynchronous computation happen-before actions following the corresponding
Future.get()
in another thread.内存一致性效果:异步计算所采取的操作发生在另一个线程中相应的
Future.get()
之后的操作之前 。
The FutureWrapper
is just a decorator for any parent Future
. FutureWrapper
只是任何父级Future
的装饰者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.