0

I am using a LinkedBlockingQueue in a multithreaded environment, where multiple threads act as consumers. Currently I am using queue.take() to get the object from queue, which is causing the thread to wait indefinitely when queue becomes empty. To avoid this can I use queue.poll() method with specified timeout with the following conditions : 1. all other threads are blocked for the specified timeout period except the current thread with call to poll(long timeout, TimeUnit unit) method.

If not, is there any way I can achieve this ?

Any leads appreciated. Thanks in advance.

Aldeguer
  • 821
  • 9
  • 32
jarvis_
  • 1
  • 1
  • Have a look at the source code for that method. You'll find `takeLock.lockInterruptibly();` and `takeLock.unlock();` - draw your conclusions from that :) – Thomas Aug 01 '17 at 11:38
  • https://stackoverflow.com/questions/8974638/blocking-queue-and-multi-threaded-consumer-how-to-know-when-to-stop – Murat Karagöz Aug 01 '17 at 11:40
  • You have it back to front. The *current* thread is blocked for the timeout period, or shorter if an element arrives, and the other threads aren't blocked at all. Your requirement is incomprehensible. – user207421 Aug 01 '17 at 11:51
  • @EJP, As per your comment, if current thread is blocked and other threads aren't blocked at all, this situation might cause current thread to starve to timeout. – jarvis_ Aug 02 '17 at 08:27

0 Answers0