有办法在java中缓冲线程池吗?
示例:我有一个最大大小为3的"BufferedThreadPool“,现在我执行5个线程。前3个线程执行。现在游泳池满了。线程4-5在执行一个线程后得到缓冲,它将执行线程4,在完成下一个线程之后,它将执行线程5,等等。
发布于 2016-05-07 00:09:16
我想你需要这样的东西:
// initialization of your threads
Runnable[] runnables = new Runnable[5];
for (int i=0; i<5; i++) {
runnables[i] = new MyRunnable(i);
}
// initialization of thread pool with fixed size 3
ExecutorService executor = Executors.newFixedThreadPool(3);
// passing the threads to the execution threads pool
for (Runnable a: runnables) {
executor.execute(a);
}我希望这能帮到你。
发布于 2016-05-06 23:44:46
Executors.newFixedThreadPool看起来像您要找的东西:https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html#newFixedThreadPool-int-
https://stackoverflow.com/questions/37082754
复制相似问题