Custom thread pool and refusal to implement the strategy

 1 package com.duchong.demo.demox.hutool.threadpool;
 2 
 3 import com.google.common.util.concurrent.ThreadFactoryBuilder;
 4 
 5 import java.util.ArrayList;
 6 import java.util.List;
 7 import java.util.concurrent.LinkedBlockingQueue;
 8 import java.util.concurrent.ThreadFactory;
 9 import java.util.concurrent.ThreadPoolExecutor;
10 import java.util.concurrent.TimeUnit;
11 
12 /**
13  * 自定义线程池
14  * @author DUCHONG
15  * @Since 2019-09-04 9:54
 16  * * / 
. 17  public  class DiyThreadPool {
 18 is  
. 19      Private  static  int maxNum = 30 ;
 20 is  
21 is      public  static  void main (String [] args) throws Exception {
 22 is          // is denied Runnable task performed by a collection of 
23          List <Runnable> rejectTask = new new ArrayList <> ();
 24-          // set the pre-created thread name format 
25          ThreadFactory threadFactory = new new. ThreadFactoryBuilder () setNameFormat ( "DiyThreadPool-% S" ) .build ();
 26 is          the ThreadPoolExecutor poolExecutor = new new the ThreadPoolExecutor (2,5,10, TimeUnit.SECONDS, new new a LinkedBlockingQueue <the Runnable> (10 ), threadFactory,
 27                  (R & lt, Executor) -> {
 28                  // the task rejected log, and then re-execute the idle time-resolved 
29                  System.out.println ( "current task is rejected:" + r.toString ());
 30                  the System.out. println ( "current thread pool size is:" + executor.getPoolSize ());
 31 is  
32                  rejectTask.add (R & lt);
 33 is              }
 34 is          );
35  
36          for ( int I =. 1; I <= maxNum; I ++ ) {
 37 [              poolExecutor.execute ( new new the Task ( "Task ---" + I));
 38 is          }
 39  
40          poolExecutor.shutdown ();
 41 is  
42 is          the System. out.println ( "need to continue to perform the task list:" );
 43 is          rejectTask.forEach (the System.out :: the println);
 44 is      }
 45  
46 is  
47      / ** 
48       * internal thread class task
 49       * / 
50      static  class the task the extends {the Thread
 51 is  
52 is         String taskName;
53         Task(String taskName){
54             super(taskName);
55             this.taskName=taskName;
56         }
57 
58         @Override
59         public void run() {
60 
61             System.out.println(Thread.currentThread().getName()+"----执行任务---"+taskName);
62         }
63     }
64 }

 

Guess you like

Origin www.cnblogs.com/geekdc/p/11462964.html