构造参数参数介绍:
corePoolSize 核心线程数量
MaximumPoolSize 最大线程数量
keepAliveTime 线程保持时间,N个时间单位
unit 时间单位(比如秒,分)
workQueue 阻塞队列
threadFactory 线程工厂
handler 线程池拒绝策略

代码示例:
public class Test001 {
public static void main(String[] args){
//创建线程池
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 2, 60L,
TimeUnit.SECONDS, new ArrayBlockingQueue < >(3));
for (int i = 1; i <= 6; i++){
TaskThred t1 = new TaskThred("任务"+ i);
//executor.execute(t1);是执行线程方法
executor.execute(t1);
}
//executor.shutdown()
//不再接受新的任务,并且等待之前提交的任务都执行完再关闭,阻塞队列中的任务不会再执行。
executor.shutdown();
}
}
class TaskThred implements Runnable {
private String taskName;
public TaskThred(String taskName){
this.taskName = taskName;
}
public void run(){
System.out.println(Thread.currentThread().getName() + taskName);
}
}
}

Was this helpful?

0 / 0

发表回复 0

Your email address will not be published.