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);
        }
  • 相关阅读:
    django中使用celery
    django中使用Redis
    Nginx编译和安装(超简单版)
    cookie和session
    forms组件
    反向解析(reverse())
    QuerySet对象
    models.py里的字段以及参数详解
    Q查询和F查询
    JgrId 无数据返回设置
  • 原文地址:https://www.cnblogs.com/zhangwanhua/p/7912176.html
Copyright © 2011-2022 走看看