簡體   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