当前位置:网站首页>Reject policy of thread pool

Reject policy of thread pool

2022-07-07 18:48:00 Gravel under Mount Everest

Using thread pool solves the process of manually creating threads , At the same time, it can also realize the reuse of threads ; Greatly save more resources and time .
We can customize different thread pools , It is convenient for submitting tasks in the future . Sometimes the number of threads we set cannot meet the usage scenario , The thread pool will handle the exceeded tasks through some strategies . Next, let's take a look at how the thread pool solves the problem of task overrun .

Reject policy parent interface of thread pool

//  Refuse to execute handler 
public interface RejectedExecutionHandler {
    
    //  When the submitted task exceeds the processing range of the thread pool ,, Maybe by ThreadPoolExecutor Method called .
    void rejectedExecution(Runnable r, ThreadPoolExecutor executor);
}

AbortPolicy( The default policy )

  //  Throw an exception RejectedExecutionException 
 public static class AbortPolicy implements RejectedExecutionHandler {
    
        /** * Creates an {@code AbortPolicy}. */
        public AbortPolicy() {
     }

        //  Throw an exception 
        // xx The task of is xx Thread pool refused 
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
    
            throw new RejectedExecutionException("Task " + r.toString() +
                                                 " rejected from " +
                                                 e.toString());
        }
    }

DiscardPolicy

//  It silently discards rejected tasks .
public static class DiscardPolicy implements RejectedExecutionHandler {
    
        /** * Creates a {@code DiscardPolicy}. */
        public DiscardPolicy() {
     }

        //  Don't do anything? , With discard task  r  The effect of 
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
    
        }
    }

DiscardOldestPolicy

//  Discard the oldest unprocessed request , And then try again execute , Unless the execution program is closed , In this case, the task is discarded .
public static class DiscardOldestPolicy implements RejectedExecutionHandler {
    
        /** * Creates a {@code DiscardOldestPolicy} for the given executor. */
        public DiscardOldestPolicy() {
     }

       //  Get and ignore  executor  The next task to be performed , If a task is immediately available , Then retry the task  r  Of   perform , Unless  executor  Shut down , In this case, the task  r  To be discarded 
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
    
           //  Check if the thread pool is closed ,
           //  If it is not closed, enter the method 
           //  Close and discard the task 
            if (!e.isShutdown()) {
    
               //  Remove the blocking queue at the last task 
                e.getQueue().poll();
                //  Put the new task submission in the queue 
                e.execute(r);
            }
        }
    }

CallerRunsPolicy

//  Directly in execute Method to run a rejected task in the calling thread of the , Unless the execution program is closed , under these circumstances , The mission will be discarded .
public static class CallerRunsPolicy implements RejectedExecutionHandler {
    
        /** * Creates a {@code CallerRunsPolicy}. */
        public CallerRunsPolicy() {
     }

        //  Execute the task in the caller's thread  r, Unless the performer has closed , In this case, the task is discarded .
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
    
        //  Check if the thread pool is closed 
        //  If it is not closed, enter the method 
        //  On the contrary, discard the task 
            if (!e.isShutdown()) {
    
            //  call run Methods to perform tasks 
            //  Execute this task in the caller thread 
                r.run();
            }
        }
    }
原网站

版权声明
本文为[Gravel under Mount Everest]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071637121766.html