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();
}
}
輸出:
注意:顯示了NetBeans 8.2中的輸出。建議在本地嘗試此示例,因為它使用multi-threading。聯機IDE服務器可能不支持此函數,或者讓您添加此類負載,從而導致超出時間限製錯誤。
相關用法
- Java LinkedTransferQueue transfer()用法及代碼示例
- Java LinkedTransferQueue removeAll()用法及代碼示例
- Java LinkedTransferQueue retainAll()用法及代碼示例
- Java LinkedTransferQueue tryTransfer()用法及代碼示例
- Java LinkedTransferQueue toString()用法及代碼示例
- Java LinkedTransferQueue forEach()用法及代碼示例
- Java LinkedTransferQueue removeIf()用法及代碼示例
- Java LinkedTransferQueue remainingCapacity()用法及代碼示例
- Java LinkedTransferQueue toArray()用法及代碼示例
- Java LinkedTransferQueue用法及代碼示例
- Java LinkedTransferQueue add()用法及代碼示例
- Java LinkedTransferQueue take()用法及代碼示例
- Java LinkedTransferQueue put()用法及代碼示例
- Java LinkedTransferQueue contains()用法及代碼示例
- Java LinkedTransferQueue drainTo()用法及代碼示例
注:本文由純淨天空篩選整理自sreeragiyer大神的英文原創作品 LinkedTransferQueue getWaitingConsumerCount() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。