LinkedTransferQueue getWaitingConsumerCount() method in Java with Examples
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.
// Java program to show LinkedTransferQueue // getWaitingConsumerCount() method import java.util.concurrent.*; public class GFG { // create object of LinkedTransferQueue LinkedTransferQueue<Integer> LTQ = new LinkedTransferQueue<>(); class Producer implements Runnable { @Override public void run() { for (int i = 0; i < 3; i++) { try { System.out.println("Producer is" + " waiting to transfer..."); // add 0, 1, 2 to LTQ LTQ.transfer(i); System.out.println("Producer " + "transferred element: " + i); // display how many consumers are waiting // after producer has transferred element System.out.println("Waiting consumer " + "count after transfer: " + LTQ .getWaitingConsumerCount()); } catch (InterruptedException e) { e.printStackTrace(); } } } } class Consumer implements Runnable { int id; Consumer(int id) { this.id = id; } @Override public void run() { try { System.out.println("Consumer " + id + " is waiting to " + "take element..."); // display how many consumers are waiting // before a consumer has taken element from LTQ System.out.println("Waiting consumer" + " count before take: " + LTQ .getWaitingConsumerCount()); Integer s = LTQ.take(); System.out.println("Consumer " + id + " received Element: " + s); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) throws InterruptedException { // class object to keep common LTQ for all consumers //(alternative: LTQ can be declared static) GFG obj = new GFG(); // enabling multiple threads ExecutorService exService = Executors.newFixedThreadPool(3); // declare one producer and 3 consumers Producer producer = obj.new Producer(); Consumer consumer0 = obj.new Consumer(0); Consumer consumer1 = obj.new Consumer(1); Consumer consumer2 = obj.new Consumer(2); // start producer and consumers exService.execute(producer); exService.execute(consumer0); exService.execute(consumer1); exService.execute(consumer2); // stop exService.shutdown(); } } |
Output:

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.
Recommended Posts:
- LinkedTransferQueue retainAll() method in Java with Examples
- LinkedTransferQueue transfer() method in Java with Examples
- LinkedTransferQueue toString() method in Java with Examples
- LinkedTransferQueue remainingCapacity() method in Java with Examples
- LinkedTransferQueue removeAll() method in Java with Examples
- LinkedTransferQueue tryTransfer() method in Java with Examples
- LinkedTransferQueue removeIf() method in Java with Examples
- LinkedTransferQueue toArray() method in Java with Examples
- LinkedTransferQueue forEach() method in Java with Examples
- LinkedTransferQueue in Java with Examples
- LinkedTransferQueue add() method in Java
- LinkedTransferQueue take() method in Java
- LinkedTransferQueue put() method in Java
- LinkedTransferQueue contains() method in Java
- LinkedTransferQueue drainTo() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



