消费者:
class ConsumerDemo extends Thread{
private Queue<Integer> queue;
private int maxSize;
private Condition lock;
private ReentrantLock reentrantLock;
public ConsumerDemo(Queue<Integer> queue, Integer size) {
this.queue = queue;
this.maxSize = size;
}
public void run() {
int i = 0;
while (++i < 30) {
synchronized (queue) {
while (queue.isEmpty()) {
System.out.println("queue is empty, consumer thread is wait...");
try {
queue.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Integer remove = queue.remove();
System.out.println("consumer value:"+remove);
queue.notify();
}
}
}
}
生产者:
class ProducerDemo extends Thread{
private Queue<Integer> queue;
private int maxSize;
public ProducerDemo(Queue<Integer> queue, Integer size) {
this.queue = queue;
this.maxSize = size;
}
public void run() {
int i = 0;
while (++i < 30) {
synchronized (queue) {
while (queue.size() == maxSize) {
System.out.println("queue is full, product thread is wait...");
try {
queue.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Random random = new Random();
int nextInt = random.nextInt(100);
System.out.println("product value:"+nextInt);
queue.add(nextInt);
queue.notify();
}
}
}
}
main方法:
public static void main(String[] args) {
Queue<Integer> qe = new ArrayDeque<>();
Thread thread1 = new Thread(new ProducerDemo(qe, 1));
thread1.start();
Thread thread2 = new Thread(new ConsumerDemo(qe, 1));
thread2.start();
}
结果:
product value:45
queue is full, product thread is wait...
consumer value:45
queue is empty, consumer thread is wait...
product value:34
queue is full, product thread is wait...
consumer value:34
queue is empty, consumer thread is wait...
product value:73
queue is full, product thread is wait...
consumer value:73
queue is empty, consumer thread is wait...
product value:7
queue is full, product thread is wait...
consumer value:7
queue is empty, consumer thread is wait...
product value:9
queue is full, product thread is wait...
consumer value:9
queue is empty, consumer thread is wait...
product value:90
queue is full, product thread is wait...
consumer value:90
queue is empty, consumer thread is wait...
product value:95
queue is full, product thread is wait...
consumer value:95
queue is empty, consumer thread is wait...
product value:43
queue is full, product thread is wait...
consumer value:43
queue is empty, consumer thread is wait...
product value:63
queue is full, product thread is wait...
consumer value:63
queue is empty, consumer thread is wait...
product value:55
queue is full, product thread is wait...
consumer value:55
queue is empty, consumer thread is wait...
product value:18
queue is full, product thread is wait...
consumer value:18
queue is empty, consumer thread is wait...
product value:64
queue is full, product thread is wait...
consumer value:64
queue is empty, consumer thread is wait...
product value:60
queue is full, product thread is wait...
consumer value:60
queue is empty, consumer thread is wait...
product value:38
queue is full, product thread is wait...
consumer value:38
queue is empty, consumer thread is wait...
product value:72
queue is full, product thread is wait...
consumer value:72
queue is empty, consumer thread is wait...
product value:5
queue is full, product thread is wait...
consumer value:5
queue is empty, consumer thread is wait...
product value:38
queue is full, product thread is wait...
consumer value:38
queue is empty, consumer thread is wait...
product value:34