2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Estojono on erityinen jono. Se noudattaa myös "first in, first out" -periaatetta.
Estojono voi olla säikeille turvallinen tietorakenne, ja sillä on seuraavat ominaisuudet:
Tyypillinen estojonon sovellusskenaario on "tuottaja-kuluttaja-malli". Tämä on hyvin tyypillinen kehitysmalli
tuottaja
Kuluttajamalli ratkaisee tuottajien ja kuluttajien välisen vahvan kytkentäongelman kontin kautta.
Tuottajat ja kuluttajat eivät ole yhteydessä toisiinsa, vaan kommunikoivat estojonojen kautta. Siksi tuottajan ei tarvitse odottaa, että kuluttaja käsittelee sen, vaan se heittää sen suoraan estojonoon ei kysy tuottajalta tietoja, vaan se otetaan suoraan estojonosta.
Tuottaja-kuluttaja-mallin toteuttamisessa on kaksi päävaihetta:
Java-standardikirjastoon on sisäänrakennettu estojono. Jos meidän on käytettävä estojonoja joissakin ohjelmissa, voimme käyttää estojonoa suoraan vakiokirjastossa.
Voi.
Estojonon pseudokoodi on seuraava:
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();
}
}