Java BlockingQueue With Timeout Option In drainTo Method
Lots of options for drainTo
with timeout option are out there.
In my option, I used a Semaphore to control the insertion and extraction of the BlockingQueue.
The main logic: for every insertion, first add
to the Queue and then use release
on the Semaphore and for every extraction, first acquire
from the Semaphore and then take
from the Queue.
For the purpose of the Gist, I extend the LinkedBlockingQueue
class and the override all the methods. Of course, they are not all needed :)