Java LinkedTransferQueue hasWaitingConsumer()方法

Java LinkedTransferQueue hasWaitingConsumer()方法

如果队列中至少有一个消费者在等待通过BlockingQueue.take()或定时轮询接收一个元素, Java.util.concurrent.LinkedTransferQueue .hasWaitingConsumer() 方法总是返回true。返回的值代表一个瞬间的状态。

语法

public boolean hasWaitingConsumer()

参数。它不接受任何参数。

返回值。该方法返回一个布尔值,如果队列中至少有一个消费者存在,该值为真。

下面是几个例子来说明LinkedTransferQueue.hasWaitingConsumer()方法。

例子1:

// Java code to demonstrate
// hasWaitingConsumer() method
  
import java.util.concurrent.LinkedTransferQueue;
  
class GFG {
    public static void main(String args[])
    {
        LinkedTransferQueue<String> LTQ
            = new LinkedTransferQueue<String>();
  
        LTQ.add("Geeks");
        LTQ.add("For");
        LTQ.add("Geeks");
        LTQ.add("GeeksForGeeks");
  
        System.out.println(LTQ.hasWaitingConsumer());
    }
}

输出:

false

例2:

// Java code to demonstrate
// hasWaitingConsumer() method
  
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedTransferQueue;
  
public class GFG {
  
    LinkedTransferQueue<Integer> LTQ
        = new LinkedTransferQueue<>();
  
    class LTQProducer implements Runnable {
  
        @Override
        public void run()
        {
            for (int i = 0; i < 3; i++) {
                try {
                    System.out.println("Producer is"
                                       + " waiting to transfer");
                    LTQ.transfer(i);
                    System.out.println("Producer transferred"
                                       + " element: "
                                       + i);
                    System.out.println("Is there any consumer"
                                       + " still waiting to"
                                       + " receive an element"
                                       + " after transfer -> "
                                       + LTQ.hasWaitingConsumer());
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
  
    class LTQConsumer implements Runnable {
        int id;
        LTQConsumer(int id)
        {
            this.id = id;
        }
  
        @Override
        public void run()
        {
            try {
                System.out.println("Consumer "
                                   + id
                                   + " is waiting to "
                                   + "take an element.");
                System.out.println("Is there any consumer"
                                   + " still waiting to"
                                   + " receive an element"
                                   + " after transfer -> "
                                   + LTQ.hasWaitingConsumer());
                Integer s = LTQ.take();
                System.out.println("Consumer "
                                   + id
                                   + " received Element: "
                                   + s);
            }
            catch (InterruptedException e) {
                System.out.println(e);
            }
        }
    }
  
    // Driver code
    public static void main(String[] args)
        throws InterruptedException
    {
        GFG ob = new GFG();
  
        ExecutorService exService
            = Executors.newFixedThreadPool(3);
  
        LTQProducer p = ob.new LTQProducer();
        LTQConsumer c1 = ob.new LTQConsumer(0);
        LTQConsumer c2 = ob.new LTQConsumer(1);
  
        exService.execute(p);
        exService.execute(c1);
        exService.execute(c2);
  
        exService.shutdown();
    }
}

输出:

Producer is waiting to transfer
Consumer 0 is waiting to take an element.
Is there any consumer waiting to take an element -> false
Consumer 1 is waiting to take an element.
Is there any consumer waiting to take an element -> false
Consumer 0 received Element: 0
Producer transferred element: 0
Is there any consumer still waiting to receive an element after transfer -> true
Producer is waiting to transfer
Producer transferred element: 1
Consumer 1 received Element: 1
Is there any consumer still waiting to receive an element after transfer -> false
Producer is waiting to transfer

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程