ThreadPoolExecutor 线程池
1.1 线程池构造方法中各个参数的意义
/**
* Creates a new {@code ThreadPoolExecutor} with the given initial parameters and default thread factory.
*
* @param corePoolSize the number of threads to keep in the pool, even if they are idle,
* unless {@code allowCoreThreadTimeOut} is set
* 核心线程数,即使空闲也会保存在线程池中,除非设置allowCoreThreadTimeOut
* @param maximumPoolSize the maximum number of threads to allow in the pool
* 线程中允许的最大线程数
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* 核心线程之外的空闲线程的回收时间
* @param unit the time unit for the {@code keepAliveTime} argument
* 上面时间的单位
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method.
* 任务队列
* @param threadFactory the factory to use when the executor creates a new thread
* 创建线程时使用的工厂,Executors提供了Executors.defaultThreadFactory()
* @param handler the handler to use when execution is blocked
* because the thread bounds and queue capacities are reached
* 当处理程序由于已达到线程边界和队列容量而阻塞时,handler被使用
* @throws IllegalArgumentException if one of the following holds:<br>
* {@code corePoolSize < 0}<br>
* {@code keepAliveTime < 0}<br>
* {@code maximumPoolSize <= 0}<br>
* {@code maximumPoolSize < corePoolSize}
* @throws NullPointerException if {@code workQueue}
* or {@code handler} is null
*/
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
threadFactory, handler);
}
测试类
package com.lhc.concurrent.executor.param;
import java.util.concurrent.*;
public class Constructor {
public static void main(String[] args) throws InterruptedException{
Runnable runnable = new Runnable(){
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " run ! " + System.currentTimeMillis());
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
}
}
};
ThreadPoolExecutor executor = getPoolBySynchronousQueue();
executor.execute(runnable);
executor.execute(runnable);
executor.execute(runnable);
executor.execute(runnable);
executor.execute(runnable);
executor.execute(runnable);
executor.execute(runnable);
executor.execute(runnable);
Thread.sleep(300);
System.out.println("first out core:" + executor.getCorePoolSize());
System.out.println("first out :" + executor.getPoolSize());
System.out.println("first out queue:" + executor.getQueue().size());
Thread.sleep(10000);
System.out.println("second out core:" + executor.getCorePoolSize());
System.out.println("second out :" + executor.getPoolSize());
System.out.println("second out queue:" + executor.getQueue().size());
}
/**
* 如果使用LinkedBlockingDeque来构造,,当线程数量大于corePoolSize时,
* 其余的任务直接放入队列中,maximumPoolSize参数的作用忽略
* keepAliveTime参数的作用忽略
* @return
*/
public static ThreadPoolExecutor getPoolByLinkedBlockingDeque(){
return new ThreadPoolExecutor(7, 8, 5,
TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>());
}
/**
* 如果使用SynchronousQueue来构造,maximumPoolSize参数的作用生效
* keepAliveTime参数的作用生效
* 当启动线程大于maximumPoolSize参数时,不会放入队列,会因为无法处理的任务直接抛出异常
* @return
*/
public static ThreadPoolExecutor getPoolBySynchronousQueue(){
return new ThreadPoolExecutor(7, 8, 5,
TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
}
}
1.2 关于 corePoolSize + LinkedBlockingDeque.size
/**
* 启动线程数大于 corePoolSize + LinkedBlockingDeque.size 时,
* 会启动非核心线程, 当启动数大于 maxSize + + LinkedBlockingDeque.size 时,
* 多出来的任务不处理,抛出异常
*/
package com.lhc.concurrent.executor.queue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class MyLinked {
public static void main(String[] args){
Runnable runnable = new Runnable(){
@Override
public void run() {
try{
System.out.println("begin " + System.currentTimeMillis());
Thread.sleep(1000);
System.out.println("end " + System.currentTimeMillis());
}catch (InterruptedException e){
e.printStackTrace();
}
}
};
LinkedBlockingDeque<Runnable> deque = new LinkedBlockingDeque<>(2);
System.out.println("deque "+ deque.size());
ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 3, 5,
TimeUnit.SECONDS, deque);
executor.execute(runnable);
executor.execute(runnable);
executor.execute(runnable);
executor.execute(runnable);
executor.execute(runnable);
System.out.println("deque " + deque.size());
System.out.println("poolSize " + executor.getPoolSize());
}
}