zoukankan      html  css  js  c++  java
  • 线程池七大参数介绍

    ExecutorService threadPool = Executors.newFixedThreadPool(5);
    
    public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }
    
    public ThreadPoolExecutor(int corePoolSize,
                                  int maximumPoolSize,
                                  long keepAliveTime,
                                  TimeUnit unit,
                                  BlockingQueue<Runnable> workQueue) {
        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             Executors.defaultThreadFactory(), defaultHandler);
    }
    
    public ThreadPoolExecutor(int corePoolSize,
                                  int maximumPoolSize,
                                  long keepAliveTime,
                                  TimeUnit unit,
                                  BlockingQueue<Runnable> workQueue,
                                  ThreadFactory threadFactory,
                                  RejectedExecutionHandler handler) {
        if (corePoolSize < 0 ||
            maximumPoolSize <= 0 ||
            maximumPoolSize < corePoolSize ||
            keepAliveTime < 0)
            throw new IllegalArgumentException();
        if (workQueue == null || threadFactory == null || handler == null)
            throw new NullPointerException();
        this.acc = System.getSecurityManager() == null ?
                null :
                AccessController.getContext();
        this.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }
    
    1. corePoolSize:线程池中常驻核心线程数
    2. maximumPoolSize:线程池能够容纳同时执行的最大线程数,此值必须大于等于1,读音[ˈmæksɪməm]咩西门-最大限度
    3. keepAliveTime:多余的空闲线程存活时间。当前线程池数量超过corePoolSize时,当空闲时间到达keepAliveTime值时,多余空闲线程会被销毁直到只剩下corePoolSize个线程为止。
    4. unit:keepAliveTime的时间单位
    5. workQueue:任务队列,被提交但尚未执行的任务
    6. threadFactory:表示生成线程池中的工作线程的线程工厂,用于创建线程,一般为默认线程工厂即可
    7. handler:拒绝策略,表示当队列满了并且工作线程大于等于线程池的最大线程数(maximumPoolSize)时如何来拒绝来请求的Runnable的策略
  • 相关阅读:
    学习之路总结
    一个怀旧的人
    struts2+ibatis+spring框架整合(一)
    大雪来的不知所措
    struts2+ibatis+spring框架整合(二)
    20110610上午java考试复数题
    直到永远……
    2012年10月份考试后感
    Use sp_MSForEachDB instead of your own loop
    Execute TSQL using OpenRowSet
  • 原文地址:https://www.cnblogs.com/xhyouyou/p/12465344.html
Copyright © 2011-2022 走看看