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.
Syntax:
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.
Output:
👁 Output in NetBeans 8.2
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.
Reference: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/LinkedTransferQueue.html#getWaitingConsumerCount--