Spring @Async

Spring @Async

Spring 3.X新增注解@async ,可以标记方法异步运行。方法可为void也可以用Future作为返回值.

如果有返回值,可以使用future.get()得到需要返回的对象.

可以使用future.get(time,unit),在指定的时间内获取返回值,如果超过设置的时间则抛出异常,异步运行.


Future> asyncResult =  presentService.generatePresent2(presentBatch, size);
try {
System.out.println(asyncResult.get(1, TimeUnit.MINUTES));
} catch (TimeoutException e) {
System.out.println("等待中...");
}
System.out.println(asyncResult.isDone());
Future> asyncResult =  presentService.generatePresent2(presentBatch, size);	 try {	 System.out.println(asyncResult.get(1, TimeUnit.MINUTES));	 } catch (TimeoutException e) {	 System.out.println("等待中...");	 }	 System.out.println(asyncResult.isDone());


Task Execution and Scheduling

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/scheduling.html

留下回复