繁体   English   中英

Java中的缓冲线程池?

[英]Buffer Thread Pool in Java?

有没有办法在Java中缓冲线程池?

示例:我有一个最大大小为3的“ BufferedThreadPool”。现在我执行5个线程。 前三个线程执行。 现在池已满。 线程4-5被缓冲在执行一个线程后,它将执行线程4,在完成下一个线程后,它将执行线程5,依此类推。

Executors.newFixedThreadPool看起来像您要找的东西: https : //docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html#newFixedThreadPool-int-

我猜你需要这样的东西:

// 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);
}

我希望这有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM