2010-09-24 173 views

回答

10

由于超时值应该由函数的调用者决定,而不是函数本身,所以@Async注释不提供超时。

我假设你指的是@Async-annotated方法返回结果的超时。 Such methods should return an instance of Future,并使用Future上的get()方法来指定超时。

例如

@Async 
public Future<String> doSomething() { 
    return new AsyncResult<String>("test"); 
} 

然后

Future<String> futureResult = obj.doSomething(); // spring makes this an async call 
String result = futureResult.get(1, TimeUnit.SECOND); 
+3

但这种方法不取消的工作线程。你有什么想法取消这个线程? – pablobaldez 2015-10-02 19:05:37

+0

@pablobaldez调用'future.cancel(true);'如果得到timeoutException – sidgate 2017-10-07 11:57:30

相关问题