当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java LinkedTransferQueue getWaitingConsumerCount()用法及代码示例


java.util.concurrent.LinkedTransferQueue.getWaitingConsumerCount()方法从LinkedTransferQueue(LTQ)返回等待通过take()或定时轮询接收元素的使用者数量。此处的消费者将阅读LTQ中的元素。生产者将向LTQ添加元素。仅当多个线程正在运行时,即生产者正在传输消息而消费者正在同时接收它们时,此方法才有用。消费者将尝试阅读LTQ的标题。如果它们不能(空LTQ或头部已被拿走),则称它们正在等待。消费者将等到他们读懂头部或经过一定的时限为止。当生产者通过transfer()添加到LTQ时,据说它被阻止了。这意味着生产者必须等到消费者读取该元素以向LTQ添加另一个元素。

用法:

public int getWaitingConsumerCount()

参数:此方法不接受任何参数。


返回值:此方法返回一个整数值,该整数值是通过take()获得的等待LTQ头部的使用者数量。

例:下面的程序使用一个生产者和3个消费者。生产者发送3条消息。首先调用take()的使用者将收到最新消息。因此,该程序的不同执行可能导致不同的使用者获得不同的消息。

// 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 in NetBeans 8.2

注意:显示了NetBeans 8.2中的输出。建议在本地尝试此示例,因为它使用multi-threading。联机IDE服务器可能不支持此函数,或者让您添加此类负载,从而导致超出时间限制错误。

参考: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/LinkedTransferQueue.html#getWaitingConsumerCount–



相关用法


注:本文由纯净天空筛选整理自sreeragiyer大神的英文原创作品 LinkedTransferQueue getWaitingConsumerCount() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。