I have just realised LinkedBlockingQueue
is a good solution in my case - I have a queue which is filled from time to time (but in very short time intervals). I want my ExecutorService
to check as often as posiible for any objects appearing in this queue.
I am not sure what is the correct use of LinkedBlockingQueue
now. Earlier, my code looked like:
public void launch(){
ListeningExecutorService pool =
MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(threadNumber));
while(true){
if(!commandsQueue.isEmpty()){
final ListenableFuture<String> future = pool.submit(new CommandWorker(commandsQueue.poll()));
// [...]
}
}
}
I am thinking about sth like:
public void launch(){
ListeningExecutorService pool =
MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(threadNumber));
while(true){
Command newCommand = commandsQueue.take();
final ListenableFuture<String> future = pool.submit(new CommandWorker(newCommand));
// [...]
}
}
The aim is, naturally, to make my ListeningExecutorService
intercept new object as fast as possible. Is this a good way?