2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
अवरोधकपङ्क्तिः एकः विशेषप्रकारस्य पङ्क्तिः अस्ति यत् एतत् "first in, first out" इति सिद्धान्तस्य अपि अनुसरणं करोति ।
अवरोधकपङ्क्तिः थ्रेड्-सुरक्षितदत्तांशसंरचना भवितुम् अर्हति तथा च निम्नलिखितलक्षणं भवति ।
कतारं अवरुद्ध्य एकं विशिष्टं अनुप्रयोगपरिदृश्यं "उत्पादक-उपभोक्तृप्रतिरूपम्" अस्ति
उत्पादकः
उपभोक्तृप्रतिरूपं उत्पादकानां उपभोक्तृणां च मध्ये दृढयुग्मसमस्यायाः समाधानं पात्रद्वारा करोति ।
उत्पादकाः उपभोक्ताश्च प्रत्यक्षतया परस्परं संवादं न कुर्वन्ति, अपितु अवरोधकपङ्क्तिद्वारा संवादं कुर्वन्ति अतः उत्पादकेन दत्तांशस्य उत्पादनानन्तरं उपभोक्तुः संसाधितुं प्रतीक्षायाः आवश्यकता नास्ति, अपितु प्रत्यक्षतया अवरोधकपङ्क्तौ क्षिपति उत्पादकं दत्तांशं न याचते तस्य स्थाने प्रत्यक्षतया अवरोधकपङ्क्तौ गृह्यते ।
उत्पादक-उपभोक्तृ-प्रतिरूपस्य कार्यान्वयनस्य द्वौ प्रमुखौ सोपानौ स्तः- १.
जावा मानकपुस्तकालये अवरुद्धपङ्क्तिः निर्मितः अस्ति यदि अस्माकं केषुचित् कार्यक्रमेषु अवरोधकपङ्क्तिः उपयोक्तव्या अस्ति तर्हि मानकपुस्तकालये अवरोधकपङ्क्तिः प्रत्यक्षतया उपयोक्तुं शक्नुमः ।
शक्नोति।
पङ्क्तिं अवरुद्ध्य छद्मसङ्केतः निम्नलिखितरूपेण अस्ति ।
BlockingQueue<String> queue = new LinkedBlockingQueue<>();
// ⼊队列
queue.put("abc");
// 出队列. 如果没有 put 直接 take, 就会阻塞.
String elem = queue.take();
public static void main(String[] args) throws InterruptedException {
BlockingQueue<Integer> blockingQueue = new LinkedBlockingQueue<Integer>();
Thread customer = new Thread(() -> {
while (true) {
try {
int value = blockingQueue.take();
System.out.println("消费元素: " + value);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "消费者");
customer.start();
Thread producer = new Thread(() -> {
Random random = new Random();
while (true) {
try {
int num = random.nextInt(1000);
System.out.println("生产元素: " + num);
blockingQueue.put(num);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "生产者");
producer.start();
customer.join();
producer.join();
}
public class BlockingQueue {
private int[] items = new int[1000];
private volatile int size = 0;
private volatile int head = 0;
private volatile int tail = 0;
public void put(int value) throws InterruptedException {
synchronized (this) {
// 此处最好使⽤ while.
// 否则 notifyAll 的时候, 该线程从 wait 中被唤醒,
// 但是紧接着并未抢占到锁. 当锁被抢占的时候, 可能⼜已经队列满了
// 就只能继续等待
while (size == items.length) {
wait();
}
items[tail] = value;
tail = (tail + 1) % items.length;
size++;
notifyAll();
}
}
public int take() throws InterruptedException {
int ret = 0;
synchronized (this) {
while (size == 0) {
wait();
}
ret = items[head];
head = (head + 1) % items.length;
size--;
notifyAll();
}
return ret;
}
public synchronized int size() {
return size;
}
// 测试代码
public static void main(String[] args) throws InterruptedException {
BlockingQueue blockingQueue = new BlockingQueue();
Thread customer = new Thread(() -> {
while (true) {
try {
int value = blockingQueue.take();
System.out.println(value);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "消费者");
customer.start();
Thread producer = new Thread(() -> {
Random random = new Random();
while (true) { try {
blockingQueue.put(random.nextInt(10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "生产者");
producer.start();
customer.join();
producer.join();
}
}