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 ??
Asked
Active
Viewed 6,442 times
3
-
Surely this is a use case thing? You use the appropriate one for the job! – Boris the Spider Jul 11 '15 at 12:32
-
@BoristheSpider I guess the OP wants to know atleast one use cases for using either of the methods. – Chetan Kinger Jul 11 '15 at 12:33
-
@CKing then, in my opinion, this question is off topic here. – Boris the Spider Jul 11 '15 at 12:34
-
@BoristheSpider In my opinion, it could be an opinion based answer. – Chetan Kinger Jul 11 '15 at 12:36
-
1`take()` is a blocking API. This means that your thread blocks at that point. `poll()` is a non-blocking API. Which means that if nothing is available you can continue with your processing. What is confusing about this? If you can not continue execution without retrieving something obviously you would need to block – Cratylus Jul 11 '15 at 15:04
1 Answers
6
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?

Solomon Slow
- 25,130
- 5
- 37
- 57