使用@Async实现异步调用
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableAsync
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.didispace.async;
|
||||
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.scheduling.annotation.AsyncResult;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
/**
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @date 16/5/16 下午12:58.
|
||||
* @blog http://blog.didispace.com
|
||||
*/
|
||||
@Component
|
||||
public class Task {
|
||||
|
||||
public static Random random =new Random();
|
||||
|
||||
@Async
|
||||
public Future<String> doTaskOne() throws Exception {
|
||||
System.out.println("开始做任务一");
|
||||
long start = System.currentTimeMillis();
|
||||
Thread.sleep(random.nextInt(10000));
|
||||
long end = System.currentTimeMillis();
|
||||
System.out.println("完成任务一,耗时:" + (end - start) + "毫秒");
|
||||
return new AsyncResult<>("任务一完成");
|
||||
}
|
||||
|
||||
@Async
|
||||
public Future<String> doTaskTwo() throws Exception {
|
||||
System.out.println("开始做任务二");
|
||||
long start = System.currentTimeMillis();
|
||||
Thread.sleep(random.nextInt(10000));
|
||||
long end = System.currentTimeMillis();
|
||||
System.out.println("完成任务二,耗时:" + (end - start) + "毫秒");
|
||||
return new AsyncResult<>("任务二完成");
|
||||
}
|
||||
|
||||
@Async
|
||||
public Future<String> doTaskThree() throws Exception {
|
||||
System.out.println("开始做任务三");
|
||||
long start = System.currentTimeMillis();
|
||||
Thread.sleep(random.nextInt(10000));
|
||||
long end = System.currentTimeMillis();
|
||||
System.out.println("完成任务三,耗时:" + (end - start) + "毫秒");
|
||||
return new AsyncResult<>("任务三完成");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.didispace;
|
||||
|
||||
import com.didispace.async.Task;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = Application.class)
|
||||
public class ApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private Task task;
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
|
||||
Future<String> task1 = task.doTaskOne();
|
||||
Future<String> task2 = task.doTaskTwo();
|
||||
Future<String> task3 = task.doTaskThree();
|
||||
|
||||
while(true) {
|
||||
if(task1.isDone() && task2.isDone() && task3.isDone()) {
|
||||
// 三个任务都调用完成,退出循环等待
|
||||
break;
|
||||
}
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
|
||||
long end = System.currentTimeMillis();
|
||||
|
||||
System.out.println("任务全部完成,总耗时:" + (end - start) + "毫秒");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user