The java.util.concurrent.LinkedTransferQueue.getWaitingConsumerCount() method returns the number of consumers waiting to receive elements via take() or timed poll, from the LinkedTransferQueue(LTQ). Consumers here will read elements from the LTQ. Producers will add elements to the LTQ. This method will be useful only when multiple threads are running, i.e, producer is transferring messages and consumers are receiving them simultaneously. Consumers will try to read the head of the LTQ. If they cannot (empty LTQ or head has already taken), they are said to be waiting. Consumers will wait till they get to read the head, or until a certain time limit elapses. When a producer adds to the LTQ via transfer(), it is said to be blocked. This means the producer has to wait until a consumer reads the element to add another element to the LTQ.
public int getWaitingConsumerCount()
Parameters: This method accepts no parameters.
Return value: This method returns an int value, which is the number of consumers awaiting the head of the LTQ, obtained via take().
Example: The program below uses one producer and 3 consumers. The producer sends 3 messages. The consumer that calls take() first, will receive the latest message. Thus, different executions of this program can lead to different consumers obtaining different messages.
Note: Output in NetBeans 8.2 is shown. It is recommended to try out this example locally because it uses multi-threading. Online IDE servers may not support this or let you add such load, leading to a time limit exceeded error.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.