简体   繁体   中英

Buffer Thread Pool in Java?

Is there a way to buffer thread pools in java?

example: I have a "BufferedThreadPool" with max size of 3. Now I execute 5 threads. The first 3 threads execute. Now the pool is full. Thread 4-5 get buffered After one thread is executed it will execute thread 4, after the next thread is done it will execute thread 5, etc.

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

I guess you need something like this:

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

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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