如果隊列中至少有一個使用方等待通過BlockingQueue.take()或定時輪詢接收元素,則Java.util.concurrent.LinkedTransferQueue.hasWaitingConsumer()方法始終返回true。返回的值表示事務的瞬時狀態。
句法
public boolean hasWaitingConsumer()
參數:它不接受任何參數。
返回值:如果隊列中至少有一個使用者,則此方法返回一個布爾值,該值為true。
下麵是一些示例來說明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
相關用法
- Java LinkedTransferQueue take()用法及代碼示例
- Java LinkedTransferQueue add()用法及代碼示例
- Java LinkedTransferQueue contains()用法及代碼示例
- Java LinkedTransferQueue put()用法及代碼示例
- Java LinkedTransferQueue isEmpty()用法及代碼示例
- Java LinkedTransferQueue spliterator()用法及代碼示例
- Java LinkedTransferQueue peek()用法及代碼示例
- Java LinkedTransferQueue poll()用法及代碼示例
- Java LinkedTransferQueue size()用法及代碼示例
- Java LinkedTransferQueue offer()用法及代碼示例
- Java LinkedTransferQueue iterator()用法及代碼示例
- Java LinkedTransferQueue drainTo()用法及代碼示例
- Java LinkedTransferQueue remove()用法及代碼示例
- Java LinkedTransferQueue transfer()用法及代碼示例
- Java LinkedTransferQueue remainingCapacity()用法及代碼示例
注:本文由純淨天空篩選整理自psil123大神的英文原創作品 LinkedTransferQueue hasWaitingConsumer() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。