66
77import java .util .Random ;
88import java .util .concurrent .CompletableFuture ;
9- import java .util .concurrent .Future ;
109
1110@ Slf4j
1211@ Component
1312public class AsyncTasks {
1413
1514 public static Random random = new Random ();
1615
17- @ Async
18- public CompletableFuture <String > doTaskOne () throws Exception {
19- log .info ("开始做任务一" );
16+ @ Async ( "taskExecutor1" )
17+ public CompletableFuture <String > doTaskOne (String taskNo ) throws Exception {
18+ log .info ("开始任务:{}" , taskNo );
2019 long start = System .currentTimeMillis ();
2120 Thread .sleep (random .nextInt (10000 ));
2221 long end = System .currentTimeMillis ();
23- log .info ("完成任务一,耗时:" + (end - start ) + "毫秒" );
24- return CompletableFuture .completedFuture ("任务一完成" );
25- }
26-
27- @ Async
28- public CompletableFuture <String > doTaskTwo () throws Exception {
29- log .info ("开始做任务二" );
30- long start = System .currentTimeMillis ();
31- Thread .sleep (random .nextInt (10000 ));
32- long end = System .currentTimeMillis ();
33- log .info ("完成任务二,耗时:" + (end - start ) + "毫秒" );
34- return CompletableFuture .completedFuture ("任务二完成" );
35- }
36-
37- @ Async
38- public CompletableFuture <String > doTaskThree () throws Exception {
39- log .info ("开始做任务三" );
40- long start = System .currentTimeMillis ();
41- Thread .sleep (random .nextInt (10000 ));
42- long end = System .currentTimeMillis ();
43- log .info ("完成任务三,耗时:" + (end - start ) + "毫秒" );
44- return CompletableFuture .completedFuture ("任务三完成" );
22+ log .info ("完成任务:{},耗时:{} 毫秒" , taskNo , end - start );
23+ return CompletableFuture .completedFuture ("任务完成" );
4524 }
4625
4726 @ Async ("taskExecutor2" )
48- public CompletableFuture <String > doTaskFour ( ) throws Exception {
49- log .info ("开始做任务四" );
27+ public CompletableFuture <String > doTaskTwo ( String taskNo ) throws Exception {
28+ log .info ("开始任务:{}" , taskNo );
5029 long start = System .currentTimeMillis ();
5130 Thread .sleep (random .nextInt (10000 ));
5231 long end = System .currentTimeMillis ();
53- log .info ("完成任务四 ,耗时:" + ( end - start ) + "毫秒" );
54- return CompletableFuture .completedFuture ("任务四完成 " );
32+ log .info ("完成任务:{} ,耗时:{} 毫秒" , taskNo , end - start );
33+ return CompletableFuture .completedFuture ("任务完成 " );
5534 }
5635
5736}
0 commit comments