为什么说不保举使用Executors创建线程池

打印 上一主题 下一主题

主题 1526|帖子 1526|积分 4578

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
起首通过executors创建的线程只有以下五种
Executors.newCachedThreadPool();

通过构造方法可以得知
无参构造

  1.            /**
  2.          * 最大线程数是 Integer的最大值
  3.          * 等待队列使用的是SynchronousQueue
  4.          *  SynchronousQueue 没有容量,等待消费者 transfer
  5.          */
  6.     public static ExecutorService newCachedThreadPool() {
  7.         return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
  8.                                       60L, TimeUnit.SECONDS,
  9.                                       new SynchronousQueue<Runnable>());
  10.     }
复制代码
有参构造

  1.           /**
  2.            * 通过参数传入线程工厂
  3.            */
  4.    public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
  5.         return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
  6.                                       60L, TimeUnit.SECONDS,
  7.                                       new SynchronousQueue<Runnable>(),
  8.                                       threadFactory);
  9.     }
复制代码
Executors.newScheduledThreadPool(5);

  1.           /**
  2.          * 核心线程数可以通过入参控制
  3.          * 最大线程数是 Integer的最大值
  4.          * 等待队列使用的是DelayedWork
  5.          *  DelayedWork 数据结果使用的是可以动态扩容的数组
  6.          */
  7.     public ScheduledThreadPoolExecutor(int corePoolSize) {
  8.         super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
  9.               new DelayedWorkQueue());
  10.     }
复制代码
Executors.newSingleThreadExecutor();

  1.           /**
  2.          * 核心线程和最大线程数都是1,只有单个线程的线程池
  3.          * 等待队列使用的是LinkedBlockingQueue
  4.          *  LinkedBlockingQueue 数据结构使用的是单向链表,同时等待队列的大小是 Integer的上限
  5.          */
  6.    public static ExecutorService newSingleThreadExecutor() {
  7.         return new FinalizableDelegatedExecutorService
  8.             (new ThreadPoolExecutor(1, 1,
  9.                                     0L, TimeUnit.MILLISECONDS,
  10.                                     new LinkedBlockingQueue<Runnable>()));
  11.     }
复制代码
Executors.newWorkStealingPool();

无参构造

  1.         /**
  2.          * 核心线程数使用的是jvm中可用的处理器数量
  3.          */
  4.     public static ExecutorService newWorkStealingPool() {
  5.         return new ForkJoinPool
  6.             (Runtime.getRuntime().availableProcessors(),
  7.              ForkJoinPool.defaultForkJoinWorkerThreadFactory,
  8.              null, true);
  9.     }
复制代码
有参构造

  1.         /**
  2.          * 核心线程数是参数大小
  3.          */
  4.     public static ExecutorService newWorkStealingPool(int parallelism) {
  5.         return new ForkJoinPool
  6.             (parallelism,
  7.              ForkJoinPool.defaultForkJoinWorkerThreadFactory,
  8.              null, true);
  9.     }
复制代码
Executors.newFixedThreadPool(10);

  1.         /**
  2.          * 核心线程数和最大线程数是一样的,通过入参配置
  3.          * LinkedBlockingQueue 数据结构使用的是单向链表,同时等待队列的大小是 Integer的上限
  4.          */
  5.    public static ExecutorService newFixedThreadPool(int nThreads) {
  6.         return new ThreadPoolExecutor(nThreads, nThreads,
  7.                                       0L, TimeUnit.MILLISECONDS,
  8.                                       new LinkedBlockingQueue<Runnable>());
  9.     }
复制代码
通过Executors创建的线程池通常并不满足我们日常开发的使用场景

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!  更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

大号在练葵花宝典

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表