zoukankan      html  css  js  c++  java
  • Java并发包之Executors

    概述

      Executor、ExecutorService、ScheduledExecutorService、ThreadFactory、Callable的工厂和工具类。

    方法

      构造一个固定线程数目的线程池,配置的corePoolSize与maximumPoolSize大小相同,同时使用了一个无界LinkedBlockingQueue存放阻塞任务,因此多余的任务将存在再阻塞队列,不会由RejectedExecutionHandler处理。

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

      构造一个缓冲功能的线程池,配置corePoolSize=0,maximumPoolSize=Integer.MAX_VALUE,keepAliveTime=60s,以及一个无容量的阻塞队列 SynchronousQueue,因此任务提交之后,将会创建新的线程执行;线程空闲超过60s将会销毁。

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

       构造一个延时线程池。

    public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
            return new ScheduledThreadPoolExecutor(corePoolSize);
        }
  • 相关阅读:
    Intern Day5
    PTA1007
    Intern Day5
    Intern Day2
    Intern Day5
    Intern Day2
    Intern Day2
    Intern Day2
    Intern Day1
    柯南剧场版17绝海的侦探
  • 原文地址:https://www.cnblogs.com/zhangwanhua/p/7912176.html
Copyright © 2011-2022 走看看