Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use poll() vs take() in ExecutorCompletionService [duplicate]

In ExecutorCompletionService we have take() and poll(). One blocks until queue has a future and other returns null if there is no future in Queue.But when to use take() vs poll() .Is there any special conditions for deciding this or we can go for any ??

like image 655
Mohit Gupta Avatar asked Sep 03 '25 03:09

Mohit Gupta


1 Answers

You use poll() when there's something else your thread could do while it is waiting for something to show up in the queue. You write a loop that calls poll(), and then do either one thing or the other depending on whether poll() returns a value or not.

Using poll() in multi-threaded code IMO is a bit of code smell. It means that you have one thread that is doing two different things. Why not use two threads in that case?

like image 159
Solomon Slow Avatar answered Sep 07 '25 05:09

Solomon Slow