是先开启corePoolSize大小的线程数,如果在来了线程就放入等待队列里,如果队列方满了,就再开启线程最大不能大于maximumPoolSize。
当前时刻最大调用量等于 maximumPoolSize+BlockingQueue.size
// 创建数组型缓冲等待队列
BlockingQueue<Runnable> bq = new ArrayBlockingQueue<Runnable>(7);
ExecutorService service = new ThreadPoolExecutor(2,4,100,TimeUnit.MILLISECONDS,bq);
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread t1 = new Thread(runnable,"线程1");
Thread t2 = new Thread(runnable,"线程2");
Thread t3 = new Thread(runnable,"线程3");
Thread t4= new Thread(runnable,"线程4");
Thread t5 = new Thread(runnable,"线程5");
Thread t6 = new Thread(runnable,"线程6");
Thread t7 = new Thread(runnable,"线程7");
Thread t8 = new Thread(runnable,"线程8");
Thread t9 = new Thread(runnable,"线程9");
service.execute(t1);
service.execute(t2);
service.execute(t3);
service.execute(t4);
service.execute(t5);
service.execute(t6);
service.execute(t7);
service.execute(t8);
service.execute(t9);
TimeUnit.SECONDS.sleep(10);
网友评论