package org.example;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
/**
* 用数组实现的阻塞队列
*/
public class ArrayBlockingQueueDemo {
ArrayBlockingQueue queue = new ArrayBlockingQueue(3);
volatile boolean isStop = false;
public static void main(String[] args) {
ArrayBlockingQueueDemo demo = new ArrayBlockingQueueDemo();
demo.test();
}
private void test() {
List<Thread> producerThreadList = new ArrayList<>();
List<Thread> consumerThreadList = new ArrayList<>();
for (int j = 0; j < 3; j++) {
producerThreadList.add((new Thread(this::producer, "包⼦⽣产商_" + j)));
}
for (int j = 0; j < 2; j++) {
consumerThreadList.add((new Thread(this::consumer, "消息者_" + j)));
}
producerThreadList.forEach(Thread::start);
consumerThreadList.forEach(Thread::start);
producerThreadList.forEach(this::threadJoin);
isStop = true;
}
private void producer() {
try {
for (int j = 0; j < 4; j++) {
System.out.println(Thread.currentThread().getName() + ", ⽣产包⼦_" + j);
queue.put("包⼦_" + j);
Thread.sleep(200);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void consumer() {
try {
while (!isStop) {
System.err.println(Thread.currentThread().getName() + ", 买到包⼦:" + queue.take());
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void threadJoin(Thread thread) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
改造后:
package org.example;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
/**
* 用数组实现的阻塞队列
*
* 如果消费者在前,生产者在后,就会出现消费者线程已经退出,又生产了几个包子,然后没有线程消费
* 主要是讲解阻塞队列的使用情况,其实你可以发现,在案例中,生产和消费基本上是持平的,没有出现疯狂生产而消费跟不上的情况。
* 具体说生产了几个,消费了几个就无所谓了
* 可以理解为阻塞队列的加入,协调了生产和消费,相当于做了限流处理
*/
public class ArrayBlockingQueueDemo1 {
ArrayBlockingQueue queue = new ArrayBlockingQueue(3);
volatile boolean isStop = false;
public static void main(String[] args) throws InterruptedException {
ArrayBlockingQueueDemo1 demo = new ArrayBlockingQueueDemo1();
demo.test();
}
private void test() throws InterruptedException {
List<Thread> producerThreadList = new ArrayList<>();
List<Thread> consumerThreadList = new ArrayList<>();
for (int j = 0; j < 3; j++) {
int finalJ = j;
producerThreadList.add((new Thread(() -> {
producer(finalJ); // finalJ
}, "包⼦⽣产商_" + j)));
}
for (int j = 0; j < 2; j++) {
consumerThreadList.add((new Thread(this::consumer, "消息者_" + j)));
}
producerThreadList.forEach(Thread::start);
consumerThreadList.forEach(Thread::start);
Thread.sleep(50000);
producerThreadList.forEach(this::threadJoin);
isStop = true;
}
private void producer(int flag) {
try {
for (int j = 0; j < 4; j++) {
System.out.println(Thread.currentThread().getName() + ", ⽣产包⼦_" + j);
queue.put("包⼦_" + flag + "=" + j);
Thread.sleep(200);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void consumer() {
try {
while (!isStop) {
System.err.println(Thread.currentThread().getName() + ", 买到包⼦:" + queue.take());
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void threadJoin(Thread thread) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
两者运行结果对比:
标签:Thread,int,void,使用,producerThreadList,new,ArrayBlockingQueue From: https://www.cnblogs.com/dongyaotou/p/18002001