- Application类添加注解启用异步任务
@EnableAsync
- 异步任务实例
package com.example.demo.task;
import java.util.Date;
import java.util.concurrent.Future;import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;@Component
@Async
public class AsyncTask {
public void task1() throws InterruptedException{
Thread.sleep(1000);
System.out.println("[task1]执行完毕,时间:" + new Date() );
}
public void task2() throws InterruptedException{
Thread.sleep(2000);
System.out.println("[task2]执行完毕,时间:" + new Date() );
}
public void task3() throws InterruptedException{
Thread.sleep(3000);
System.out.println("[task3]执行完毕,时间:" + new Date() );
}
public Future<String> task4() throws InterruptedException{
Thread.sleep(1000);
System.out.println("[task4]执行完毕,时间:" + new Date() );
return new AsyncResult<String>("[task4]执行完毕,时间:" + new Date());
}
public Future<String> task5() throws InterruptedException{
Thread.sleep(2000);
System.out.println("[task5]执行完毕,时间:" + new Date() );
return new AsyncResult<String>("[task5]执行完毕,时间:" + new Date());
}
public Future<String> task6() throws InterruptedException{
Thread.sleep(3000);
System.out.println("[task6]执行完毕,时间:" + new Date() );
return new AsyncResult<String>("[task6]执行完毕,时间:" + new Date());
}}
- Controller类
package com.example.demo.controller;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import com.example.demo.domain.JsonData;
import com.example.demo.task.AsyncTask;@RestController
@RequestMapping("/async")
public class AsyncTaskController {@Autowired
private AsyncTask asyncTask;
/**
* 功能描述: 异步任务测试1,只异步调用无需返回值
* @return
* @throws InterruptedException
*/
@RequestMapping("/test1")
public Object test1() throws InterruptedException{
long begin = System.currentTimeMillis();
asyncTask.task1();
asyncTask.task2();
asyncTask.task3();
long end = System.currentTimeMillis();
System.out.println("执行完毕,消耗时间="+ (end-begin));
return JsonData.buildSuccess() ;
}
/**
* 功能描述: 异步任务测试2,异步调用并获取返回值
* @return
* @throws InterruptedException
* @throws ExecutionException
*/
@RequestMapping("/test2")
public Object test2() throws InterruptedException, ExecutionException{
long begin = System.currentTimeMillis();
Future<String> task4= asyncTask.task4();
Future<String> task5= asyncTask.task5();
Future<String> task6= asyncTask.task6();
while( !(task4.isDone()&&task5.isDone()&&task6.isDone()) ){ }
long end = System.currentTimeMillis();
System.out.println("执行完毕,消耗时间="+ (end-begin));
System.out.println(task4.get());
return JsonData.buildSuccess(end-begin) ;
}
}