简体   繁体   English

你会在App Engine中使用什么“未来”?

[英]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 form Future<?> and return null 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 of Future that implements Runnable , and so may be executed by an Executor . 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.

相关问题 您将使用哪些Java框架与Google App Engine合作? - What Java framework would you use with Google App Engine? 构建一个大型Java应用程序,您将使用什么堆栈? - building a high scale java app, what stack would you use? 如果App Engine自动将stdout和stderr记录到INFO&WARNING中,为什么还要使用logging.properties? - Why would you use logging.properties if App Engine automatically logs stdout & stderr to INFO & WARNING? appfuse vs roo - 你会用什么? - appfuse vs roo - what would you use 您将在Java中使用什么部署脚本? - What would you use for deployment scripts in Java? 在Google App Engine下可以运行哪些WordPress类似物\\克隆? - What are the WordPress analogs\clones that would run under Google App Engine? 未来一段时间在App Engine上执行任务的最有效方法是什么? - What's the most effective way to execute a task some distant time in the future on App Engine? 您将使用什么语言/平台来共享软件/免费软件桌面应用程序? - What language / platform would you use for a shareware / freeware desktop application? 您可以将Java EE框架与Google App Engine一起使用吗? - Can you use Java EE frameworks with the Google App Engine? 为什么要在App Engine上设置系统属性(例如java.vm.specification.version)? - Why would you want to set system properties such as java.vm.specification.version on App Engine?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM