I need to provide a method that blocks until all outstanding work in BlockingQueue is processed.
I thought I could handle this by counting a semaphore that starts at 0 and decreases as items are added to the queue and increase as they complete. finish () just acquired a semaphore, let it go and left.
Perhaps I could call reducePermits (). Does this work if the number of permissions is already <0? It is protected, so I need to extend the Semaphore class to make it work.
My second best idea is to check the contents of the queue in a loop and sleep for 100 ms between checks. It works, but it seems kludgey.
It makes sense? Does anyone have an easier way to suggest?TIA - Tim.
public MyClass {
public class MySemaphore extends Semaphore {
public void seize() {
reducePermits(1);
}
}
private MySemaphore allDone = new MySemaphore();
void startSomething() {
allDone.seize();
}
void finishSomething() {
allDone.release();
}
void finish() {
allDone.acquire();
allDone.release();
}
}