Thread Pool


from: Executor Framework in Java

Executor

This interface has a single method (execute) to execute a Runnable task.

    public interface Executor {
        void execute(Runnable command);
    }

ExecutorService

It supplements base class (Executor) by adding more methods to control life cycle.

    shutdown();
    submit();
    invokeAll();
    invokeAny();
    ...

AbstractExecutorService

Implement sumbit/invoke functions

    submit();//it will call execute()
    invokeAll();
    invokeAny();

ThreadPoolExecutor

  • Thread pool constructor

      public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize,long
                                keepAliveTime,TimeUnit unit,
                                BlockingQueue<Runnable> workQueue);
    
      public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long 
                                keepAliveTime,TimeUnit unit,
                                BlockingQueue<Runnable> workQueue,
                                ThreadFactory threadFactory);
    
      public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long         
                                keepAliveTime,TimeUnit unit,
                                BlockingQueue<Runnable> workQueue,
                                RejectedExecutionHandler handler);
    
      public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long     
                                keepAliveTime,TimeUnit unit,
                                BlockingQueue<Runnable> workQueue,
                                ThreadFactory threadFactory,
                                RejectedExecutionHandler handler);
    
    • corePoolSize
    • maximumPoolSize
    • keepAliveTime
    • workQueue
    • RejectedExecutionHandler
  • Implement function

      execute()
      shutdown();
      shutdownNow();
      ...
    
  • Flow
    from: java.util.concurrent並發包諸類概覽
    線程池具備這樣的優先級處理策略:

    1. 請求到來首先交給coreSize內的常駐線程執行
    2. 如果coreSize的線程全忙,任務被放到隊列裡面
    3. 如果隊列放滿了,會新增線程,直到達到maxSize
    4. 如果還是處理不過來,會把一個異常扔到RejectedExecutionHandler中去,
      用戶可以自己設定這種情況下的最終處理策略

Executors

A factory to create ThreadPoolExecutor

  1. Executors.newCachedThreadPool()
     // maximumPoolSize = Integer.MAX_VALUE
     public static ExecutorService newCachedThreadPool() {
         return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                       60L, TimeUnit.SECONDS,
                                       new SynchronousQueue<Runnable>());
     }
    
  2. Executors.newSingleThreadExecutor()
     // corePoolSize = maximumPoolSize = 1
     public static ExecutorService newSingleThreadExecutor() {
         return new FinalizableDelegatedExecutorService
             (new ThreadPoolExecutor(1, 1,
                                     0L, TimeUnit.MILLISECONDS,
                                     new LinkedBlockingQueue<Runnable>()));
     }
    
  3. Executors.newFixedThreadPool(int)
     // corePoolSize = maximumPoolSize = nThreads
     public static ExecutorService newFixedThreadPool(int nThreads) {
         return new ThreadPoolExecutor(nThreads, nThreads,
                                       0L, TimeUnit.MILLISECONDS,
                                       new LinkedBlockingQueue<Runnable>());
     }
    

results matching ""

    No results matching ""