Skip to content

Commit 679ebf6

Browse files
committed
使用@async实现异步任务
1 parent 1228430 commit 679ebf6

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,46 @@
11
package com.didispace.chapter75;
22

3-
import lombok.AllArgsConstructor;
43
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.scheduling.annotation.Async;
55
import org.springframework.stereotype.Component;
66

77
import java.util.Random;
8+
import java.util.concurrent.CompletableFuture;
89

910
@Slf4j
1011
@Component
11-
@AllArgsConstructor
1212
public class AsyncTasks {
1313

1414
public static Random random = new Random();
1515

16-
public void doTaskOne() throws Exception {
16+
@Async
17+
public CompletableFuture<String> doTaskOne() throws Exception {
1718
log.info("开始做任务一");
1819
long start = System.currentTimeMillis();
1920
Thread.sleep(random.nextInt(10000));
2021
long end = System.currentTimeMillis();
2122
log.info("完成任务一,耗时:" + (end - start) + "毫秒");
23+
return CompletableFuture.completedFuture("任务一完成");
2224
}
2325

24-
public void doTaskTwo() throws Exception {
26+
@Async
27+
public CompletableFuture<String> doTaskTwo() throws Exception {
2528
log.info("开始做任务二");
2629
long start = System.currentTimeMillis();
2730
Thread.sleep(random.nextInt(10000));
2831
long end = System.currentTimeMillis();
2932
log.info("完成任务二,耗时:" + (end - start) + "毫秒");
33+
return CompletableFuture.completedFuture("任务二完成");
3034
}
3135

32-
public void doTaskThree() throws Exception {
36+
@Async
37+
public CompletableFuture<String> doTaskThree() throws Exception {
3338
log.info("开始做任务三");
3439
long start = System.currentTimeMillis();
3540
Thread.sleep(random.nextInt(10000));
3641
long end = System.currentTimeMillis();
3742
log.info("完成任务三,耗时:" + (end - start) + "毫秒");
43+
return CompletableFuture.completedFuture("任务三完成");
3844
}
3945

4046
}

2.x/chapter7-5/src/main/java/com/didispace/chapter75/Chapter75Application.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.scheduling.annotation.EnableAsync;
56

7+
@EnableAsync
68
@SpringBootApplication
79
public class Chapter75Application {
810

2.x/chapter7-5/src/test/java/com/didispace/chapter75/Chapter75ApplicationTests.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.boot.test.context.SpringBootTest;
77

8+
import java.util.concurrent.CompletableFuture;
9+
import java.util.concurrent.Future;
10+
811
@Slf4j
912
@SpringBootTest
1013
public class Chapter75ApplicationTests {
@@ -14,9 +17,17 @@ public class Chapter75ApplicationTests {
1417

1518
@Test
1619
public void test() throws Exception {
17-
asyncTasks.doTaskOne();
18-
asyncTasks.doTaskTwo();
19-
asyncTasks.doTaskThree();
20+
long start = System.currentTimeMillis();
21+
22+
CompletableFuture<String> task1 = asyncTasks.doTaskOne();
23+
CompletableFuture<String> task2 = asyncTasks.doTaskTwo();
24+
CompletableFuture<String> task3 = asyncTasks.doTaskThree();
25+
26+
CompletableFuture.allOf(task1, task2, task3).join();
27+
28+
long end = System.currentTimeMillis();
29+
30+
log.info("任务全部完成,总耗时:" + (end - start) + "毫秒");
2031
}
2132

2233
}

0 commit comments

Comments
 (0)