ThreadPoolExecutor custom creates a thread pool using a bounded queue

  When using a bounded queue, if there is a new task to be executed, if the actual number of threads in the thread pool is less than corePoolSize, the thread will be created first,
  if it is greater than corePoolSize, the task will be added to the queue,
  if the queue is full, the total number of threads Under the premise of not greater than maximumPoolSize, create a new thread,

  If the number of threads is greater than maximumPoolSize, the rejection policy is executed. or other custom ways.

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;
public class UseThreadPoolExecutor1 {





   public static void main (String[] args) {
       /**
        * When using a bounded queue, if there is a new task to be executed, if the actual number of threads in the thread pool is less than corePoolSize, the thread will be created first,
        * if it is greater than corePoolSize, then The task will be added to the queue.
        * If the queue is full, a new thread will be created on the premise that the total number of threads is not greater than the maximumPoolSize.
        * If the number of threads is greater than the maximumPoolSize, the rejection policy will be executed. or other custom ways.
       * 
        */    
       ThreadPoolExecutor pool = new ThreadPoolExecutor(
             2 ,               //coreSize
 3 ,               //MaxSize
 60 ,           //60
 TimeUnit. SECONDS , 
 // new ArrayBlockingQueue<Runnable>(2) //Specify a queue (bounded queue)
 new                                                LinkedBlockingQueue<Runnable>()
            , new MyRejected()
            //, new DiscardOldestPolicy()
            );
MyTask mt1 = new MyTask(1, "任务1");
MyTask mt2 = new MyTask(2, "任务2");
MyTask mt3 = new MyTask(3, "任务3");
MyTask mt4 = new MyTask(4, "任务4");
MyTask mt5 = new MyTask(5, "任务5"      
                              );
      MyTask mt6 = new MyTask(6, "任务6");
pool.execute(mt1);
pool.execute(mt2);
pool.execute(mt3);
pool.execute(mt4);
pool.execute(mt5);
pool.execute(mt6);
pool.shutdown();
}      
                                          
         
}
public class MyTask implements Runnable {

   private int taskId;
   private String taskName;
   
   public MyTask(int taskId, String taskName){
      this.taskId = taskId;
      this.taskName = taskName;
   }
   
   public int getTaskId() {
      return taskId;
   }

   public void setTaskId(int taskId) {
      this.taskId = taskId;
   }

   public String getTaskName() {
      return taskName;
   }

   public void setTaskName(String taskName) {
      this.taskName = taskName;
   }

   @Override
   public void run() {
      try {
         System.out.println("run taskId =" + this.taskId+",Thread1"+Thread.currentThread().getName());
         Thread.sleep(5*1000);
         //System.out.println("end taskId =" + this.taskId);
      } catch (InterruptedException e) {
         e.printStackTrace();
      }     
   }
   
   public String toString(){
      return Integer.toString(this.taskId);
   }

}
import java.net.HttpURLConnection;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

public class MyRejected implements RejectedExecutionHandler{

   
   public MyRejected(){
   }
   
   @Override
   public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
      System.out.println("自定义处理..");
      System.out.println("当前被拒绝任务为:" + r.toString());
      

   }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324822881&siteId=291194637