The denial policy describes the denial policy of the thread pool. It refers to the action taken when a task is denied when it is added to the thread pool. The reason why a task is rejected when it is added to the thread pool is: first, the thread pool is shut down abnormally. Second, the number of tasks exceeds the maximum limit of the thread pool. The thread pool contains four denial policies: AbortPolicy, CallerRunsPolicy, DiscardOldestPolicy, and DiscardPolicy. AbortPolicy -- when a task is rejected when it is added to the thread pool, it throws a RejectedExecutionException. CallerRunsPolicy-when a task is rejected when it is added to the Thread pool, the rejected task is processed in the Thread pool currently running in the Thread pool. DiscardOldestPolicy-when a task is denied when it is added to the thread pool, the thread pool will discard the oldest unprocessed task in the waiting queue and then add the rejected task to the waiting queue. DiscardPolicy-when a task is added to the thread pool and is denied, the thread pool discards the rejected task. The default processing policy of the thread pool is AbortPolicy! Comparison and examples of denial policies the following uses an example to demonstrate the four denial policies of the thread pool. 1. discardPolicy Example 2. discardOldestPolicy example 3. example of AbortPolicy 4. callerRunsPolicy Example 1. discardPolicy example copy code 1 import java. lang. reflect. field; 2 import java. util. concurrent. arrayBlockingQueue; 3 import java. util. concurrent. threadPoolExecutor; 4 import java. util. concurrent. timeUnit; 5 import java. util. concurrent. threadPoolExecutor. discardPolicy; 6 7 public class DiscardPolicyDemo {8 9 private stati C final int THREADS_SIZE = 1; 10 private static final int CAPACITY = 1; 11 12 public static void main (String [] args) throws Exception {13 14 // create a thread pool. The "maximum pool size" and "core pool size" of the thread pool are both 1 (THREADS_SIZE), and the blocking queue CAPACITY of the "Thread Pool" is 1 (CAPACITY ). 15 ThreadPoolExecutor pool = new ThreadPoolExecutor (THREADS_SIZE, THREADS_SIZE, 0, TimeUnit. SECONDS, 16 new ArrayBlockingQueue <Runnable> (CAPACITY); 17 // set the thread pool rejection policy to "discard" 18 pool. setRejectedExecutionHandler (new ThreadPoolExecutor. discardPolicy (); 19 20 // create 10 tasks and add them to the thread pool. 21 for (int I = 0; I <10; I ++) {22 Runnable myrun = new MyRunnable ("task-" + I); 23 pool.exe cute (myrun ); 24} 25 // close the thread pool 26 pool. shutdown (); 27} 28} 29 30 class MyRunnable implements Runnable {31 private String name; 32 public MyRunnable (String name) {33 this. name = name; 34} 35 @ Override36 public void run () {37 try {38 System. out. println (this. name + "is running. "); 39 Thread. sleep (100); 40} catch (Limit t Ion e) {41 e. printStackTrace (); 42} 43} 44} copy the code execution result: task-0 is running. task-1 is running. results: The "maximum pool size" and "core pool size" of the thread pool are both 1 (THREADS_SIZE), which means that "the maximum number of tasks that the thread pool can run simultaneously is 1 ". The blocking queue of the thread pool is ArrayBlockingQueue, and ArrayBlockingQueue is a bounded blocking queue. The capacity of ArrayBlockingQueue is 1. This also means that only one thread pool can be blocked in the thread pool blocking queue. According to the execute () code analyzed in "", two tasks are run in the thread pool. 1st tasks are directly put into the Worker and executed through the thread; 2nd tasks are put in the blocked queue for waiting. Other tasks are discarded! 2. discardOldestPolicy example copy code 1 import java. lang. reflect. field; 2 import java. util. concurrent. arrayBlockingQueue; 3 import java. util. concurrent. threadPoolExecutor; 4 import java. util. concurrent. timeUnit; 5 import java. util. concurrent. threadPoolExecutor. discardOldestPolicy; 6 7 public class DiscardOldestPolicyDemo {8 9 private static final int THREADS_SIZE = 1; 10 private static final int CAPACITY = 1; 11 12 public static void main (String [] args) throws Exception {13 14 // create a thread pool. The "maximum pool size" and "core pool size" of the thread pool are both 1 (THREADS_SIZE), and the blocking queue CAPACITY of the "Thread Pool" is 1 (CAPACITY ). 15 ThreadPoolExecutor pool = new ThreadPoolExecutor (THREADS_SIZE, THREADS_SIZE, 0, TimeUnit. SECONDS, 16 new ArrayBlockingQueue <Runnable> (CAPACITY); 17 // set the thread pool rejection policy to "DiscardOldestPolicy" 18 pool. setRejectedExecutionHandler (new ThreadPoolExecutor. discardOldestPolicy (); 19 20 // create 10 tasks and add them to the thread pool. 21 for (int I = 0; I <10; I ++) {22 Runnable myrun = new MyRunnable ("task-" + I); 23 pool.exe cute (myrun ); 24} 25 // close the thread pool 26 pool. shutdown (); 27} 28} 29 30 class MyRunnable implements Runnable {31 private String name; 32 public MyRunnable (String name) {33 this. name = name; 34} 35 @ Override36 public void run () {37 try {38 System. out. println (this. name + "is running. "); 39 Thread. sleep (200); 40} catch (Limit t Ion e) {41 e. printStackTrace (); 42} 43} 44} copy the code execution result: task-0 is running. task-9 is running. result Description: after the "Thread Pool rejection policy" is changed from DiscardPolicy to DiscardOldestPolicy, when a task is added to the thread pool and is denied, the thread pool discards the task at the end of the blocking queue, then add the rejected task to the end. 3. abortPolicy example copy code 1 import java. lang. reflect. field; 2 import java. util. concurrent. arrayBlockingQueue; 3 import java. util. concurrent. threadPoolExecutor; 4 import java. util. concurrent. timeUnit; 5 import java. util. concurrent. threadPoolExecutor. abortPolicy; 6 import java. util. concurrent. rejectedExecutionException; 7 8 public class AbortPolicyDemo {9 10 private static final int THREADS_SIZE = 1; 1 1 private static final int CAPACITY = 1; 12 13 public static void main (String [] args) throws Exception {14 15 // create a thread pool. The "maximum pool size" and "core pool size" of the thread pool are both 1 (THREADS_SIZE), and the blocking queue CAPACITY of the "Thread Pool" is 1 (CAPACITY ). 16 ThreadPoolExecutor pool = new ThreadPoolExecutor (THREADS_SIZE, THREADS_SIZE, 0, TimeUnit. SECONDS, 17 new ArrayBlockingQueue <Runnable> (CAPACITY); 18 // set the thread pool rejection policy to "throw an exception" 19 pool. setRejectedExecutionHandler (new ThreadPoolExecutor. abortPolicy (); 20 21 try {22 23 // create 10 tasks and add them to the thread pool. 24 for (int I = 0; I <10; I ++) {25 Runnable myrun = new MyRunnable ("task-" + I); 26 pool.exe cute (myrun ); 27} 28} catch (RejectedExecutionException e) {29 e. printStackTrace (); 30 // closes the 31 pool of the thread pool. shutdown (); 32} 33} 34} 35 36 class MyRunnable implements Runnable {37 private String name; 38 public MyRunnable (String name) {39 this. name = name; 40} 41 @ Override42 public void run () {43 try {44 System. out. print Ln (this. name + "is running. "); 45 Thread. sleep (200); 46} catch (Exception e) {47 e. printStackTrace (); 48} 49} 50} copy the code (one time) running result: copy the code 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. threadPoolExec Utor.exe cute (ThreadPoolExecutor. java: 656) at AbortPolicyDemo. main (AbortPolicyDemo. java: 27) task-0 is running. task-1 is running. description of the Code copying Result: After the "Thread Pool rejection policy" is modified from DiscardPolicy to AbortPolicy, A RejectedExecutionException will be thrown when a task is added to the thread pool for rejection. 4. callerRunsPolicy sample copy code 1 import java. lang. reflect. field; 2 import java. util. concurrent. arrayBlockingQueue; 3 import java. util. concurrent. threadPoolExecutor; 4 import java. util. concurrent. timeUnit; 5 import java. util. concurrent. threadPoolExecutor. callerRunsPolicy; 6 7 public class CallerRunsPolicyDemo {8 9 private static final int THREADS_SIZE = 1; 10 private static final int CAPACITY = 1; 11 1 2 public static void main (String [] args) throws Exception {13 14 // create a thread pool. The "maximum pool size" and "core pool size" of the thread pool are both 1 (THREADS_SIZE), and the blocking queue CAPACITY of the "Thread Pool" is 1 (CAPACITY ). 15 ThreadPoolExecutor pool = new ThreadPoolExecutor (THREADS_SIZE, THREADS_SIZE, 0, TimeUnit. SECONDS, 16 new ArrayBlockingQueue <Runnable> (CAPACITY); 17 // set the thread pool rejection policy to "CallerRunsPolicy" 18 pool. setRejectedExecutionHandler (new ThreadPoolExecutor. callerRunsPolicy (); 19 20 // create 10 tasks and add them to the thread pool. 21 for (int I = 0; I <10; I ++) {22 Runnable myrun = new MyRunnable ("task-" + I); 23 pool.exe cute (myrun ); 24} 25 26 // close the thread pool 27 pool. shutdown (); 28} 29} 30 31 class MyRunnable implements Runnable {32 private String name; 33 public MyRunnable (String name) {34 this. name = name; 35} 36 @ Override37 public void run () {38 try {39 System. out. println (this. name + "is running. "); 40 Thread. sleep (100); 41} catch (Exception e) {42 e. printStackTrace (); 43} 44} 45} copy the code (one time) running result: copy the code 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.