Thread Pool (2) -Executors provide four thread pool

 

1. Why not use Executors provide four thread pool thread pool that creates

Alibaba open manual written:

4 . [Forced] thread pool Executors are not allowed to create, but by ThreadPoolExecutor way, this approach allows the students to write more explicit operating rules thread pool, to avoid the risk of resource depletion. 
Description: Executors drawbacks thread pool objects returned as follows: 
. 1 ) and FixedThreadPool SingleThreadPool: request queue length allowed Integer.MAX_VALUE, may accumulate a large number of requests, thereby causing OOM.
2 ) CachedThreadPool and ScheduledThreadPool: allow the number of threads to create Integer.MAX_VALUE, may create a large number of threads, resulting OOM.

2. Create process

Although it is not recommended to use Executors create a thread pool, but still have to figure out his principles.

public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }
public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }

note:

  1. Core = maximum thread pool thread pool (not hire temporary workers)

  2.LinkedBlockingQueue according to FIFO queue, the queue is maximum Integer.MAX_VALUE

public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }

note:

  1. There is no core thread pool, only a maximum thread pool (all hired temporary workers to work, work was done on all dismissed, not authentic)

  2. The maximum thread pool Integer.MAX_VALUE

  3.SynchronousQueue is a blocking queue in which each put must wait for a take, that is, if there are two threads wait take, queues can save two, if not wait take, can not be stored in the queue.

public ScheduledThreadPoolExecutor(int corePoolSize) {
        super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
              new DelayedWorkQueue());
    }

note:

  1. The maximum thread pool Integer.MAX_VALUE

  2. The thread pool is not yet understood, delayed

Guess you like

Origin www.cnblogs.com/SmilingEye/p/11751302.html