拒絕策略介紹
線程池的拒絕策略,是指當任務添加到線程池中被拒絕,而采取的處理措施。
當任務添加到線程池中之所以被拒絕,可能是由于:第一,線程池異常關閉。第二,任務數量超過線程池的最大限制。
線程池共包括4種拒絕策略,它們分別是:AbortPolicy, CallerRunsPolicy, DiscardOldestPolicy和DiscardPolicy。
- AbortPolicy -- 當任務添加到線程池中被拒絕時,它將拋出 RejectedExecutionException 異常。
- CallerRunsPolicy -- 當任務添加到線程池中被拒絕時,會在線程池當前正在運行的Thread線程池中處理被拒絕的任務。
- DiscardOldestPolicy -- 當任務添加到線程池中被拒絕時,線程池會放棄等待隊列中最舊的未處理任務,然后將被拒絕的任務添加到等待隊列中。
- DiscardPolicy -- 當任務添加到線程池中被拒絕時,線程池將丟棄被拒絕的任務。
線程池默認的處理策略是AbortPolicy!
拒絕策略對比和示例
下面通過示例,分別演示線程池的4種拒絕策略。
1. DiscardPolicy 示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
import java.lang.reflect.Field; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy; public class DiscardPolicyDemo { private static final int THREADS_SIZE = 1 ; private static final int CAPACITY = 1 ; public static void main(String[] args) throws Exception { // 創建線程池。線程池的"最大池大小"和"核心池大小"都為1(THREADS_SIZE),"線程池"的阻塞隊列容量為1(CAPACITY)。 ThreadPoolExecutor pool = new ThreadPoolExecutor(THREADS_SIZE, THREADS_SIZE, 0 , TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(CAPACITY)); // 設置線程池的拒絕策略為"丟棄" pool.setRejectedExecutionHandler( new ThreadPoolExecutor.DiscardPolicy()); // 新建10個任務,并將它們添加到線程池中。 for ( int i = 0 ; i < 10 ; i++) { Runnable myrun = new MyRunnable( "task-" +i); pool.execute(myrun); } // 關閉線程池 pool.shutdown(); } } class MyRunnable implements Runnable { private String name; public MyRunnable(String name) { this .name = name; } @Override public void run() { try { System.out.println( this .name + " is running." ); Thread.sleep( 100 ); } catch (Exception e) { e.printStackTrace(); } } } |
運行結果:
1
2
|
task-0 is running. task-1 is running. |
結果說明:線程池pool的"最大池大小"和"核心池大小"都為1(THREADS_SIZE),這意味著"線程池能同時運行的任務數量最大只能是1"。
線程池pool的阻塞隊列是ArrayBlockingQueue,ArrayBlockingQueue是一個有界的阻塞隊列,ArrayBlockingQueue的容量為1。這也意味著線程池的阻塞隊列只能有一個線程池阻塞等待。
根據""中分析的execute()代碼可知:線程池中共運行了2個任務。第1個任務直接放到Worker中,通過線程去執行;第2個任務放到阻塞隊列中等待。其他的任務都被丟棄了!
2. DiscardOldestPolicy 示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
import java.lang.reflect.Field; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy; public class DiscardOldestPolicyDemo { private static final int THREADS_SIZE = 1 ; private static final int CAPACITY = 1 ; public static void main(String[] args) throws Exception { // 創建線程池。線程池的"最大池大小"和"核心池大小"都為1(THREADS_SIZE),"線程池"的阻塞隊列容量為1(CAPACITY)。 ThreadPoolExecutor pool = new ThreadPoolExecutor(THREADS_SIZE, THREADS_SIZE, 0 , TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(CAPACITY)); // 設置線程池的拒絕策略為"DiscardOldestPolicy" pool.setRejectedExecutionHandler( new ThreadPoolExecutor.DiscardOldestPolicy()); // 新建10個任務,并將它們添加到線程池中。 for ( int i = 0 ; i < 10 ; i++) { Runnable myrun = new MyRunnable( "task-" +i); pool.execute(myrun); } // 關閉線程池 pool.shutdown(); } } class MyRunnable implements Runnable { private String name; public MyRunnable(String name) { this .name = name; } @Override public void run() { try { System.out.println( this .name + " is running." ); Thread.sleep( 200 ); } catch (Exception e) { e.printStackTrace(); } } } |
運行結果:
1
2
|
task-0 is running. task-9 is running. |
結果說明:將"線程池的拒絕策略"由DiscardPolicy修改為DiscardOldestPolicy之后,當有任務添加到線程池被拒絕時,線程池會丟棄阻塞隊列中末尾的任務,然后將被拒絕的任務添加到末尾。
3. AbortPolicy 示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
import java.lang.reflect.Field; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.ThreadPoolExecutor.AbortPolicy; import java.util.concurrent.RejectedExecutionException; public class AbortPolicyDemo { private static final int THREADS_SIZE = 1 ; private static final int CAPACITY = 1 ; public static void main(String[] args) throws Exception { // 創建線程池。線程池的"最大池大小"和"核心池大小"都為1(THREADS_SIZE),"線程池"的阻塞隊列容量為1(CAPACITY)。 ThreadPoolExecutor pool = new ThreadPoolExecutor(THREADS_SIZE, THREADS_SIZE, 0 , TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(CAPACITY)); // 設置線程池的拒絕策略為"拋出異常" pool.setRejectedExecutionHandler( new ThreadPoolExecutor.AbortPolicy()); try { // 新建10個任務,并將它們添加到線程池中。 for ( int i = 0 ; i < 10 ; i++) { Runnable myrun = new MyRunnable( "task-" +i); pool.execute(myrun); } } catch (RejectedExecutionException e) { e.printStackTrace(); // 關閉線程池 pool.shutdown(); } } } class MyRunnable implements Runnable { private String name; public MyRunnable(String name) { this .name = name; } @Override public void run() { try { System.out.println( this .name + " is running." ); Thread.sleep( 200 ); } catch (Exception e) { e.printStackTrace(); } } } |
(某一次)運行結果:
1
2
3
4
5
6
7
|
java.util.concurrent.RejectedExecutionException at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1774) at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:768) at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:656) at AbortPolicyDemo.main(AbortPolicyDemo.java:27) task-0 is running. task-1 is running. |
結果說明:將"線程池的拒絕策略"由DiscardPolicy修改為AbortPolicy之后,當有任務添加到線程池被拒絕時,會拋出RejectedExecutionException。
4. CallerRunsPolicy 示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
import java.lang.reflect.Field; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; public class CallerRunsPolicyDemo { private static final int THREADS_SIZE = 1 ; private static final int CAPACITY = 1 ; public static void main(String[] args) throws Exception { // 創建線程池。線程池的"最大池大小"和"核心池大小"都為1(THREADS_SIZE),"線程池"的阻塞隊列容量為1(CAPACITY)。 ThreadPoolExecutor pool = new ThreadPoolExecutor(THREADS_SIZE, THREADS_SIZE, 0 , TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(CAPACITY)); // 設置線程池的拒絕策略為"CallerRunsPolicy" pool.setRejectedExecutionHandler( new ThreadPoolExecutor.CallerRunsPolicy()); // 新建10個任務,并將它們添加到線程池中。 for ( int i = 0 ; i < 10 ; i++) { Runnable myrun = new MyRunnable( "task-" +i); pool.execute(myrun); } // 關閉線程池 pool.shutdown(); } } class MyRunnable implements Runnable { private String name; public MyRunnable(String name) { this .name = name; } @Override public void run() { try { System.out.println( this .name + " is running." ); Thread.sleep( 100 ); } catch (Exception e) { e.printStackTrace(); } } } |
(某一次)運行結果:
1
2
3
4
5
6
7
8
9
10
|
task-2 is running. task-3 is running. task-4 is running. task-5 is running. task-6 is running. task-7 is running. task-8 is running. task-9 is running. task-0 is running. task-1 is running. |
結果說明:將"線程池的拒絕策略"由DiscardPolicy修改為CallerRunsPolicy之后,當有任務添加到線程池被拒絕時,線程池會將被拒絕的任務添加到"線程池正在運行的線程"中取運行