Overview
This chapter describes the deny policy for the thread pool. The content includes:
Introduction to Reject Policies
Rejection policy comparisons and examples
Reprint Please specify source: http://www.cnblogs.com/skywang12345/p/3512947.html
Introduction to Reject policies
The thread pool's deny policy refers to the actions taken when a task is added to the thread pool and is rejected.
When a task is added to the thread pool it is rejected, possibly because: first, the thread pool closes unexpectedly. Second, the number of tasks exceeds the maximum limit of the thread pool.
The thread pool consists of 4 rejection strategies, namely:abortpolicy, callerrunspolicy, discardoldestpolicy , and Discardpolicy.
AbortPolicy --It throws a Rejectedexecutionexception exception when the task is added to the thread pool and is rejected. Callerrunspolicy --When a task is added to the thread pool, the rejected task is processed in the thread pool that is currently running by the online pool. --When the task is added to the thread pool for rejection, the thread pool discards the oldest unhandled task in the queue, and then adds the rejected task to the wait queue. Discardpolicy --When a task is added to the thread pool and is rejected, the thread pool discards the rejected task.
The thread pool default processing policy is abortpolicy!
rejection policy comparisons and examples
The following example shows the 4 deny policies for the thread pool, respectively.
1. Discardpolicy Example
2. Discardoldestpolicy Example
3. AbortPolicy Example
4. Callerrunspolicy Example
1. Discardpolicy Example
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 static final int threads_size = 1;10 private static final int C apacity = 1;11 public static void Main (string[] args) throws Exception {13 14//Create thread pool. The maximum pool size and core pool size for the thread pool are 1 (threads_size), and the thread pool's blocking queue capacity is 1 (capacity). Threadpoolexecutor pool = new Threadpoolexecutor (threads_size, threads_size, 0, timeunit.seconds,16 New arrayblockingqueue<runnable> (capacity)); 17//Set the Deny policy for the thread pool to "discard" Pool.setrejectedexecutionha Ndler (New Threadpoolexecutor.discardpolicy ()); 19 20//Create a new 10 tasks and add them to the thread pool. for (int i = 0; i < i++) {Runnable myrun = new Myrunnable ("task-" +i); Execute (myrun); 24}25//Close thread pool Pool.shutdown ();}28}29 class Myrunnable implements Runnable {+ Private stri ng name;32 Public myrunnable (String name) {this.name = name;34}35 @Override36 public void Run ( ) {PNs try {System.out.println (THIS.name + "is running"); Thread.Sleep (Exception e) {e.printstacktrace (); 42}43} 44}
Operation result :
Task-0 is Running.task-1 is running.
The result : The maximum pool size and core pool size for the thread pool pool are 1 (threads_size), which means that the maximum number of tasks that the thread pool can run concurrently is only 1.
The blocking queue for the thread pool pool is arrayblockingqueue,arrayblockingqueue is a bounded blocking queue with a capacity of 1 arrayblockingqueue. This also means that the thread pool's blocking queue can only have one thread pool blocking the wait.
According to the Execute () code analyzed in the, the thread pool runs 2 tasks. The 1th task is placed directly in the worker, executed through a thread, and the 2nd task is placed in the blocking queue. All the other tasks have been discarded!
2. Discardoldestpolicy Example
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 public static void Main (string[] args) throws Exception {13 14//Create thread pool. The maximum pool size and core pool size for the thread pool are 1 (threads_size), and the thread pool's blocking queue capacity is 1 (capacity). Threadpoolexecutor pool = new Threadpoolexecutor (threads_size, threads_size, 0, timeunit.seconds,16 New arrayblockingqueue<runnable> (capacity)); 17//Set the Deny policy for the thread pool to "Discardoldestpolicy" pool.setre Jectedexecutionhandler (New Threadpoolexecutor.discardoldestpolicy ()); 19 20//Create a new 10 tasks and add them to the thread pool. for (int i = 0; i < i++) {Runnable myrun = new Myrunnable ("task-" +i); 23 Pool.execute (Myrun); 24}25//Close thread pool Pool.shutdown ();}28}29 class myrunnable imple ments Runnable {$ private String name;32 public myrunnable (String name) {this.name = name;34}35 @Override36 public void Run () {PNs try {System.out.println (THIS.name + "is running."); Thread.Sleep (Exception e) {e.printstacktrace (); 42}43} 44}
Operation result :
Task-0 is running.task-9 is running.
result Note : After modifying the thread pool's deny policy from Discardpolicy to Discardoldestpolicy, when a task is added to the thread pool is rejected, the thread pool discards the task at the end of the blocking queue and then adds the rejected task to the end.
3. AbortPolicy Example
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 private static final int threads_size = 1;11 private static final int CAP acity = 1;12 public static void Main (string[] args) throws Exception {14 15//Create thread pool. The maximum pool size and core pool size for the thread pool are 1 (threads_size), and the thread pool's blocking queue capacity is 1 (capacity). Threadpoolexecutor pool = new Threadpoolexecutor (threads_size, threads_size, 0, timeunit.seconds,17 New arrayblockingqueue<runnable> (capacity)); 18//Set the Deny policy for the thread pool to "throw exception" pool.setrejectedexecution Handler (New Threadpoolexecutor.abortpolicy ()); try {22 23//Create a new 10 tasks and add them to the thread pool. (int i = 0; i < i++) {RunnAble Myrun = new myrunnable ("task-" +i); Pool.execute (Myrun);}28} catch (rejected Executionexception e) {e.printstacktrace (); 30//Close thread pool Pool.shutdown (); 32 }33}34}35 class Myrunnable implements Runnable {Notoginseng private String name;38 public myrunnable (String name) {this.name = name;40}41 @Override42 public void Run () {# try {System.out. println (THIS.name + "is running."); Thread.Sleep (Exception e) {e.printstacktrace (); 48}49} 50}
Run results (one time) :
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.
result Description : After modifying the thread pool's deny policy from Discardpolicy to AbortPolicy, a rejectedexecutionexception is thrown when a task is added to the thread pool that is rejected.
4. Callerrunspolicy Example
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 in T capacity = 1;11 public static void Main (string[] args) throws Exception {13 14//Create thread pool. The maximum pool size and core pool size for the thread pool are 1 (threads_size), and the thread pool's blocking queue capacity is 1 (capacity). Threadpoolexecutor pool = new Threadpoolexecutor (threads_size, threads_size, 0, timeunit.seconds,16 New arrayblockingqueue<runnable> (capacity)); 17//Set the Deny policy for the thread pool to "Callerrunspolicy" Pool.setrejec Tedexecutionhandler (New Threadpoolexecutor.callerrunspolicy ()); 19 20//Create a new 10 tasks and add them to the thread pool. for (int i = 0; i < i++) {Runnable myrun = new Myrunnable ("task-" +i); ExeCute (myrun); 24}25 26//Close thread pool Pool.shutdown ();}29}30 class Myrunnable implements Run nable {name;33-myrunnable (string name) {this.name = name;35}36 @Overr Ide37 public void Run () {$ try {System.out.println (THIS.name + "is running."); Thread.Sleep (Exception e) {e.printstacktrace (); 43}44} 45}
Run results (one time) :
Task-2 is running.task-3 are running.task-4 is running.task-5 are running.task-6 is running.task-7 Ing.task-9 is running.task-0 are Running.task-1 is running.
result Note : After modifying the thread pool's deny policy from Discardpolicy to Callerrunspolicy, when a task is added to the thread pool is rejected, the thread pool adds the rejected task to the thread pool running threads to run.
Java Multithreading series: Juc thread pool: 05 thread pool principle (d) (EXT)