高性能ワークスレッドを構築し、Springスレッドを拡張します.

4559 ワード

Springスレッドを拡張
/**
  SPRING 2.0 threadPooltaskExecutor
    theadPoolexuecutor   ,      CorePoolSize、MaximumPoolSize、KeepAliveTime    。
*/
public class MyThreadPoolTaskExecutor implements SchedulingTaskExecutor,Executor, InitializingBean, DisposableBean {
    //           
    private int corePoolSize;
    //         
    private int maxPoolSize;
    //        
    private int keepAliveSeconds;
    //      
   private int queueCapacity;
    //     
   private String threadPoolName;
    //thread   
   private ThreadFactory threadFactory;
    //        
   private RejectedExecutionHandler rejectedExecutionHandler;
    //jdk       
   private ThreadPoolExecutor executorService;
    //        
   private final Object poolSizeMonitor = new Object();
    //MAP          
   private static ConcurrentHashMap threadPoolMap = new 
                 ConcurrentHashMap <String,ThreadPoolExecutor>();

    //      
    public MyThreadPoolTaskExecutor() {
         corePoolSize = 1;
         maxPoolSize = 2147483647;
         keepAliveSeconds = 60;
         queueCapacity = 2147483647;
         threadFactory = Executors.defaultThreadFactory();
         rejectedExecutionHandler = new 
                 java.util.concurrent.ThreadPoolExecutor.AbortPolicy();
         threadPoolName ="";
    }

    //      
    protected BlockingQueue createQueue(int queueCapacity) {
        if (this.queueCapacity > 0)
            return new LinkedBlockingQueue(this.queueCapacity);
        else
            return new SynchronousQueue();
    }
 
   //       
   public void execute(Runnable task) {
         Assert.notNull(executorService,"ThreadPoolTaskExecutor not    initialized");
         executorService.execute(task);
   }

   //SPRING        ,             CorePoolSize、
   //MaximumPoolSize、KeepAliveTime    。 
    public ThreadPoolExecutor getThreadPoolExecutor() throws IllegalStateException {
      Assert.state(this.executorService != null, "ThreadPoolTaskExecutor not initialized");
      return this.executorService;
   }
   public void setCorePoolSize(int corePoolSize) {
      synchronized (this.poolSizeMonitor) {
         this.corePoolSize = corePoolSize;
         if (this.executorService != null) {
               this.executorService.setCorePoolSize(corePoolSize);
         }
      }
   }
   public int getCorePoolSize() {
      synchronized (this.poolSizeMonitor) {
         return this.executorService.getCorePoolSize();
      }
   }
   public void setMaxPoolSize(int maxPoolSize) {
      synchronized (this.poolSizeMonitor) {
         this.maxPoolSize = maxPoolSize;
         if (this.executorService != null) {
            this.executorService.setMaximumPoolSize(maxPoolSize);
         }
      }
   }
   public int getMaxPoolSize() {
      synchronized (this.poolSizeMonitor) {
         return this.executorService.getMaximumPoolSize();
      }
   }
   public void setKeepAliveSeconds(int keepAliveSeconds) {
      synchronized (this.poolSizeMonitor) {
         this.keepAliveSeconds = keepAliveSeconds;
         if (this.executorService != null) {
            this.executorService.setKeepAliveTime(keepAliveSeconds,TimeUnit.SECONDS);
         }
      }
   }

   public int getKeepAliveSeconds() {
      synchronized (this.poolSizeMonitor) {
         return this.keepAliveSeconds;
      }
   }

   public void setQueueCapacity(int queueCapacity) {
      this.queueCapacity = queueCapacity;
    }

   public void setThreadFactory(ThreadFactory threadFactory) {
      this.threadFactory = threadFactory == null ? Executors
.defaultThreadFactory() : threadFactory;
   }

   public void setRejectedExecutionHandler(
      RejectedExecutionHandler rejectedExecutionHandler) {
         this.rejectedExecutionHandler = ((RejectedExecutionHandler)(rejectedExecutionHandler == null ? 
((RejectedExecutionHandler) (new java.util.concurrent.ThreadPoolExecutor.AbortPolicy()))
: rejectedExecutionHandler));
      }

}