通过创建线程池执行器的构造方法去手动创建一个自己的线程池.
代码语言:javascript复制package ThreadPoll;
/*
创建一个线程池
corePoolSize=2,maximumPoolSize=5,keepAliveTime=1,keepAliveTime=TimeUnit.SECONDS
,workQueue=new LinkedBlockingQueue<Runnable>(3),threadFactory=Executors.defaultThreadFactory()
handler=new ThreadPoolExecutor.CallerRunsPolicy());
*/
import java.util.concurrent.*;
public class MyThreadPool {
public static void main(String[] args){
ExecutorService threadPool= //自定义线程池
new ThreadPoolExecutor(
2,
5,
1,
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(3),//任务队列容量设为3
Executors.defaultThreadFactory()
,new ThreadPoolExecutor.CallerRunsPolicy());//自定义拒绝策略
for(int i=1;i<=10;i ){
int d=i;
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName() "办理业务" d);//模拟处理业务
});
}
}
}