美文网首页
Java并发(六) -- Executors源码简析

Java并发(六) -- Executors源码简析

作者: OakesYa | 来源:发表于2020-05-21 10:44 被阅读0次

    背景

    Executors类和上面的Executor接口看起来很像,就增加了一个s字符,但是功能完全不一样。我们可以看下源码的注释是这样描述的

    Factory and utility methods for {@link Executor}, {@link
    ExecutorService}, {@link ScheduledExecutorService}, {@link
    ThreadFactory}, and {@link Callable} classes defined in this
    package
    

    翻译一下就是Executors类为Executor,ExecutorService,ScheduledExecutorService,ThreadFactory,Callable提供工厂和实用方法,可以看看源码学习一下

    源码(精简版)

    public class Executors {
        // 创建一个指定个数线程池
        public static ExecutorService newFixedThreadPool(int nThreads) {
            return new ThreadPoolExecutor(nThreads, nThreads,
                                          0L, TimeUnit.MILLISECONDS,
                                          new LinkedBlockingQueue<Runnable>());
        }
    
        //增加一个ForkJoinPool的线程池
        public static ExecutorService newWorkStealingPool(int parallelism) {
            return new ForkJoinPool
                (parallelism,
                 ForkJoinPool.defaultForkJoinWorkerThreadFactory,
                 null, true);
        }
        public static ExecutorService newWorkStealingPool() {
            return new ForkJoinPool
                (Runtime.getRuntime().availableProcessors(),
                 ForkJoinPool.defaultForkJoinWorkerThreadFactory,
                 null, true);
        }
    
        // 创建只包括一个线程的线程池
        public static ExecutorService newSingleThreadExecutor() {
            return new FinalizableDelegatedExecutorService
                (new ThreadPoolExecutor(1, 1,
                                        0L, TimeUnit.MILLISECONDS,
                                        new LinkedBlockingQueue<Runnable>()));
        }
    
        // 创建一个可缓存的线程池
        public static ExecutorService newCachedThreadPool() {
            return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                          60L, TimeUnit.SECONDS,
                                          new SynchronousQueue<Runnable>());
        }
    
        // 创建一个计划执行的线程
        public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
            return new DelegatedScheduledExecutorService
                (new ScheduledThreadPoolExecutor(1));
        }
    
        //默认的线程工厂
        static class DefaultThreadFactory implements ThreadFactory {
            private static final AtomicInteger poolNumber = new AtomicInteger(1);
            private final ThreadGroup group;
            private final AtomicInteger threadNumber = new AtomicInteger(1);
            private final String namePrefix;
    
            DefaultThreadFactory() {
                SecurityManager s = System.getSecurityManager();
                group = (s != null) ? s.getThreadGroup() :
                                      Thread.currentThread().getThreadGroup();
                namePrefix = "pool-" +
                              poolNumber.getAndIncrement() +
                             "-thread-";
            }
    
            public Thread newThread(Runnable r) {
                Thread t = new Thread(group, r,
                                      namePrefix + threadNumber.getAndIncrement(),
                                      0);
                if (t.isDaemon())
                    t.setDaemon(false);
                if (t.getPriority() != Thread.NORM_PRIORITY)
                    t.setPriority(Thread.NORM_PRIORITY);
                return t;
            }
        }
    }
    

    我们可以看到线程池都是返回一个ThreadPoolExecutor对象,上面可以看到创建了五种线程池,然后线程池执行我们可以等看下ThreadPoolExecutor

    相关文章

      网友评论

          本文标题:Java并发(六) -- Executors源码简析

          本文链接:https://www.haomeiwen.com/subject/eanuohtx.html