暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Java创建线程池的方法

码上记录 2021-09-19
598

Java有两种方法来创建线程池。一种是使用ThreadPoolExecutor 类创建一个线程池,一种是使用Executors类来创建一个线程池。


一 使用ThreadPoolExecutor创建一个线程池


如下所示,ThreadPoolExecutor 类有四个构造方法

    //5个构造参数
    ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, 
    long keepAliveTime,TimeUnit unit,BlockingQueue<Runnable> workQueue)


    //6个构造参数
    ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, 
    TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler)


    //6个构造参数
    ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, 
    TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory)


    //7个构造参数
    ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, 
    TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, 
    RejectedExecutionHandler handler)


    其中最长的一个构造方法有7个构造参数。这个7个构造参数分别是


    2个和线程数相关的参数


    corePoolSize核心线程数,线程池中始终存活的线程数
    maximumPoolSize最大线程数,线程池中允许的最大线程数


    2个和线程存活时间相关的参数


    keepAliveTime存活时间,线程没有任务执行时最多保持多久时间会终止
    unit参数keepAliveTime的时间单位,7种可选


    unit参数选项含义
    TimeUnit.DAYS
    TimeUnit.HOURS小时
    TimeUnit.MINUTES
    TimeUnit.SECONDS
    TimeUnit.MILLISECONDS毫秒
    TimeUnit.MICROSECONDS微秒
    TimeUnit.NANOSECONDS纳秒


    1个阻塞队列参数:workQueue,用来存储等待执行的任务,是线程安全的,有7个选项。


    workQueue参数选项含义
    ArrayBlockingQueue一个由数组结构组成的有界阻塞队列。
    LinkedBlockingQueue一个由链表结构组成的有界阻塞队列。
    SynchronousQueue一个不存储元素的阻塞队列,即直接提交给线程不保持它们。
    PriorityBlockingQueue一个支持优先级排序的无界阻塞队列。
    DelayQueue一个使用优先级队列实现的无界阻塞队列,只有在延迟期满时才能从中提取元素。
    LinkedTransferQueue一个由链表结构组成的无界阻塞队列。与SynchronousQueue类似,还含有非阻塞方法。
    LinkedBlockingDeque一个由链表结构组成的双向阻塞队列。


    1个线程工厂参数:threadFactory。主要用来创建线程。


    1个拒绝策略参数:handler。主要用来表示拒绝处理任务的策略。有4个选项可选。


    handler参数选项含义
    AbortPolicy拒绝并抛出异常
    CallerRunsPolicy重试提交当前的任务,即再次调用运行该任务的execute()方法
    DiscardOldestPolicy抛弃队列头部(最旧)的一个任务,并执行当前任务
    DiscardPolicy抛弃当前任务


    二  使用Executors类来创建一个线程池。


    查看Executors的API,发现它可以创建4种类型的线程池,每种类型的线程池都有几个方法可以创建。下面来逐一介绍。


    CachedThreadPool:  可缓存的线程池,若线程数超过处理所需,缓存一段时间会回收,如线程数不够,则新建线程。


    CachedThreadPool创建方法

      static ExecutorService  newCachedThreadPool()
      static ExecutorService newCachedThreadPool(ThreadFactory threadFactory)


      FixedThreadPool:固定大小的线程池。可控制并发的线程数,超出线程会在队列中等待。


      FixedThreadPool创建方法

         static ExecutorService newFixedThreadPool(int nThreads)
        static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory)


        ScheduledThreadPool:创建一个周期性的线程池,支持定时及周期性执行任务。


        ScheduledThreadPool创建方法

          static ScheduledExecutorService  newScheduledThreadPool(int corePoolSize)
          static ScheduledExecutorService  newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory)


          需要注意的是,ScheduledExecutorService执行任务使用的是schedule()方法,而不是ExecutorService类的execute()方法。


          SingleThreadExecutor单线程的线程池,可保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。


          SingleThreadExecutor创建方法

            static ExecutorService  newSingleThreadExecutor()
            static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory)


            文章转载自码上记录,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

            评论