线程池各个参数含义
- int corePoolSize, 核心线程数
- int maximumPoolSize, 最大线程数
- long keepAliveTime, 空闲线程存活时间
- TimeUnit unit, 存活时间的单位
- BlockingQueue workQueue, 存放线程任务队列
- ThreadFactory threadFactory, 线程工厂,创建新线程
- RejectedExecutionHandler handler 拒绝策略,线程池拒绝处理后的任务
注意:要执行的逻辑(Callable或者Runnable)是先交给workQueue,然后才是由线程池去指派线程执行workQueue中的任务,不是调用execute或者submit方法,就直接执行了。
关闭线程池的方法:
package day09.part2;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 创建线程的开销是非常大的,为了更好的复用线程,
* 提高效率。(就像高铁一样)
*
* 接口:Executor
* 子接口:ExecutorService
* 实现抽象类:AbstractExecutorService
* 子类:ThreadPoolExecutor
*
* 线程池各个参数含义:
* int corePoolSize, 核心线程数
* int maximumPoolSize, 最大线程数
* long keepAliveTime, 空闲线程存活时间
* TimeUnit unit, 存活时间的单位
* BlockingQueue<Runnable> workQueue, 存放线程任务队列
* ThreadFactory threadFactory, 线程工厂,创建新线程
* RejectedExecutionHandler handler 拒绝策略,线程池拒绝处理后的任务
*
* 注意:要执行的逻辑(Callable或者Runnable)是先交给workQueue,
* 然后才是由线程池去指派线程执行workQueue中的任务,不是调用execute或者submit方法
* 就直接执行了。
*
*
*
* @author xzq
*/
public class ExecutorTest01 {
public static void main(String[] args) {
ThreadPoolExecutor threadPool = createThreadPool();
int activeCount =-1;
int queueSize = -1;
while(true){
if(activeCount!=threadPool.getActiveCount()||queueSize!=threadPool.getQueue().size()){
System.out.println("线程池核心线程数是:"+threadPool.getCorePoolSize());
int activeTempCount = threadPool.getActiveCount();
int queueTempSize = threadPool.getQueue().size();
System.out.println("线程池活跃线程数是:"+activeTempCount);
System.out.println("线程池最大线程数是:"+threadPool.getMaximumPoolSize());
System.out.println("线程池阻塞任务队列大小是:"+queueTempSize);
activeCount=activeTempCount;
queueSize=queueTempSize;
}
}
}
/**
* 创建线程池方法
* @return
*/
private static ThreadPoolExecutor createThreadPool() {
//创建一个阻塞队列 可以设置队列的大小
BlockingQueue<Runnable> blcQ=new ArrayBlockingQueue<>(1);
//创建线程工厂
ThreadFactory threadFactory=new MyThreadFactory();
//创建拒绝策略
RejectedExecutionHandler handler=new ThreadPoolExecutor.AbortPolicy();
ThreadPoolExecutor threadPool=new ThreadPoolExecutor(
1, 2, 5L, TimeUnit.SECONDS, blcQ, threadFactory, handler);
System.out.println("线程池创建完毕---------------------------------");
threadPool.execute(new Runnable() {
@Override
public void run() {
sleepSeconds(1);//改为30
}
});
//-------------演示核心线程执行所有任务
threadPool.execute(new Runnable() {
@Override
public void run() {
sleepSeconds(1);
}
});
//-------------演示新创建线程
threadPool.execute(new Runnable() {
@Override
public void run() {
sleepSeconds(1);
}
});
/*
* 休眠8秒 演示空闲超时时间,
* 核心线程执行任务30秒,超时5秒,休眠8秒
*/
/*try {
Thread.sleep(8_000);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
//-------------演示执行拒绝策略
/* threadPool.execute(new Runnable() {
@Override
public void run() {
sleepSeconds(1);
}
});*/
/*
* 8秒后放两个任务进去,使它创建新的线程,
* 观察与原有非核心线程是否是同一个线程
*/
/*threadPool.execute(new Runnable() {
@Override
public void run() {
sleepSeconds(1);
}
});*/
return threadPool;
}
private static void sleepSeconds(int seconds){
try {
System.out.println(Thread.currentThread().getName()+"正在执行……");
Thread.sleep(seconds*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static class MyThreadFactory implements ThreadFactory{
private AtomicInteger threadNum=new AtomicInteger();
@Override
public Thread newThread(Runnable r) {
threadNum.getAndIncrement();//threadNum++
return new Thread(r, "线程池中的线程"+threadNum+":");
// return new Thread(r);
}
}
}