NOTE

6.28 Executors

1. Usage 2. newCachedThreadPool 3. newScheduledThreadPool 4. newFixedThreadPool 5. newSingleThreadExecutor

JavaCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. Usage

public class TestExecutors
{
    public static void main(String[] args)
    {
        Executors.newCachedThreadPool();
        Executors.newScheduledThreadPool(10, Executors.defaultThreadFactory());
        Executors.newFixedThreadPool(10);
        Executors.newSingleThreadExecutor();

    }
}

2. newCachedThreadPool

public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,// There is a problem with the maximum thread count
                                  60L, TimeUnit.SECONDS,
                                  new SynchronousQueue<Runnable>());
}

3. newScheduledThreadPool

public static ScheduledExecutorService newScheduledThreadPool(
    int corePoolSize, ThreadFactory threadFactory) {
return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
}

public ScheduledThreadPoolExecutor(int corePoolSize,
                                   ThreadFactory threadFactory) {
   // There is a problem with the maximum thread count
    super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
          new DelayedWorkQueue(), threadFactory);
}

4. newFixedThreadPool

public static ExecutorService newFixedThreadPool(int nThreads) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>());// The LinkedBlockingQueue used here is unbounded
}

5. newSingleThreadExecutor

public static ExecutorService newSingleThreadExecutor() {
    return new FinalizableDelegatedExecutorService
        (new ThreadPoolExecutor(1, 1,
                                0L, TimeUnit.MILLISECONDS,
                                new LinkedBlockingQueue<Runnable>()));// The LinkedBlockingQueue used here is unbounded
}

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub