Reputation: 2451
I have 4-5 worker threads processing large message queue. And I also have another piece of code which runs using 2-3 workers. I wanted to block all other workers when large message queue is being processed.
I am using JDK 6, and Jms
EDITED:
Queue process workers never terminated. They blocked on queue when no message. These workers are managed by executor thread pool.If I use read-write lock, one of these workers will get blocked too. Also, if used cyclic barrier then I have to terminate threads in order to relase the blocking second process. Since workers are managed by thread pool, it is not assured that all workers will be busy processing messages.
Let me know,
final ExecutorService executor = getExecutorManager().getExecutor();
for (int i = 0; i < threadPoolSize; i++) {
executor.submit(new MessageWorker(qConn));
}
Following is second module, where i want all workers to be blocked while queue processors worker threads are working.
final ExecutorService executor = getExecutorManager().getExecutor();
for (int i = 0; i < threadPoolSize; i++) {
executor.submit(new DbUpdateWorker());
}
Upvotes: 1
Views: 725
Reputation: 60758
You need to use CyclicBarrier.
A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point. CyclicBarriers are useful in programs involving a fixed sized party of threads that must occasionally wait for each other. The barrier is called cyclic because it can be re-used after the waiting threads are released.
Use as:
CyclicBarrier barrier = new CyclicBarrier(numWorkers, runnable);
Where runnable
is a Runnable
that you want to call when your worker threads are finished. Each thread calls barrier.await()
when it's complete.
Upvotes: 1