Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaLinkedTransferQueue getWaitingConsumerCount() method in Java with Examples

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:
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–

RELATED ARTICLES

Most Popular

Recent Comments