File tree Expand file tree Collapse file tree 2 files changed +23
-12
lines changed
2.x/chapter7-8/src/main/java/com/didispace/chapter78 Expand file tree Collapse file tree 2 files changed +23
-12
lines changed Original file line number Diff line number Diff line change @@ -23,14 +23,4 @@ public CompletableFuture<String> doTaskOne(String taskNo) throws Exception {
23
23
return CompletableFuture .completedFuture ("任务完成" );
24
24
}
25
25
26
- @ Async ("taskExecutor2" )
27
- public CompletableFuture <String > doTaskTwo (String taskNo ) throws Exception {
28
- log .info ("开始任务:{}" , taskNo );
29
- long start = System .currentTimeMillis ();
30
- Thread .sleep (random .nextInt (10000 ));
31
- long end = System .currentTimeMillis ();
32
- log .info ("完成任务:{},耗时:{} 毫秒" , taskNo , end - start );
33
- return CompletableFuture .completedFuture ("任务完成" );
34
- }
35
-
36
26
}
Original file line number Diff line number Diff line change 12
12
import org .springframework .web .bind .annotation .RestController ;
13
13
14
14
import java .util .concurrent .Executor ;
15
+ import java .util .concurrent .RejectedExecutionHandler ;
15
16
import java .util .concurrent .ThreadPoolExecutor ;
16
17
17
18
@ EnableAsync
@@ -35,8 +36,28 @@ public Executor taskExecutor1() {
35
36
executor .setKeepAliveSeconds (60 );
36
37
executor .setThreadNamePrefix ("executor-1-" );
37
38
38
- // 配置拒绝策略
39
- executor .setRejectedExecutionHandler (new ThreadPoolExecutor .CallerRunsPolicy ());
39
+ /**配置拒绝策略**/
40
+
41
+ // AbortPolicy策略:默认策略,如果线程池队列满了丢掉这个任务并且抛出RejectedExecutionException异常。
42
+ // executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
43
+
44
+ // DiscardPolicy策略:如果线程池队列满了,会直接丢掉这个任务并且不会有任何异常。
45
+ // executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
46
+
47
+ // DiscardOldestPolicy策略:如果队列满了,会将最早进入队列的任务删掉腾出空间,再尝试加入队列。
48
+ // executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
49
+
50
+ // CallerRunsPolicy策略:如果添加到线程池失败,那么主线程会自己去执行该任务,不会等待线程池中的线程去执行。
51
+ // executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
52
+
53
+ // 自定义策略
54
+ // executor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
55
+ // @Override
56
+ // public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
57
+ //
58
+ // }
59
+ // });
60
+
40
61
return executor ;
41
62
}
42
63
You can’t perform that action at this time.
0 commit comments