首页>代码>spring通过@Async注解实现异步执行Service方法并且通过Java Future对象得到执行结果>/spring-async-future-example/src/main/java/com/memorynotfound/MailSender.java
package com.memorynotfound;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
import java.util.concurrent.Future;
@Component
public class MailSender {
@Async
public Future<Boolean> sendMail() throws InterruptedException {
System.out.println("sending mail..");
Thread.sleep(1000 * 10);
System.out.println("sending mail completed");
return new AsyncResult<Boolean>(true);
}
}

最近下载
最近浏览
