生产者是一堆线程,消费者是另一堆线程,内存缓冲区可以使用List数组队列,数据类型只需要定义一个简单的类就好。关键是如何处理多线程之间的协作。这其实也是多线程通信的一个范例。
通过wait/notify实现生产者消费者
package com.zmysna.corresponse;
import java.util.LinkedList;
/*
消费者
*/
public class Comsumer implements Runnable {
private LinkedList<Integer> queue;
public Comsumer(LinkedList<Integer> queue) {
this.queue = queue;
}
@Override
public void run() {
try {
while (true) {
synchronized (queue) {
if (queue.isEmpty()) {
queue.notifyAll();
queue.wait();
}
else System.out.println(Thread.currentThread().getName() + "消费了" + queue.removeFirst());
}
Thread.sleep(2000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.zmysna.corresponse;
import java.util.LinkedList;
import java.util.Random;
/*
生产者
*/
public class Producer implements Runnable {
private LinkedList<Integer> queue;
private int length;
public Producer(LinkedList<Integer> queue, int length) {
this.queue = queue;
this.length = length;
}
@Override
public void run() {
try {
while (true) {
Random rd = new Random();
synchronized (queue) {
if (queue.size() == length) {
queue.notifyAll();
queue.wait();
}
int data = rd.nextInt(100);
System.out.println(Thread.currentThread().getName() + "开始生产数字" + data);
queue.addLast(data);
Thread.sleep(1000);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.zmysna.corresponse;
import java.util.LinkedList;
public class Test01 {
public static void main(String[] args) {
LinkedList<Integer> queue = new LinkedList<>();
Producer producer01 = new Producer(queue,1);
Comsumer comsumer01 = new Comsumer(queue);
new Thread(producer01, "生产者01:").start();
new Thread(comsumer01, "消费者03:").start();
new Thread(comsumer01, "消费者04:").start();
}
}
通过await/signal实现生产者消费者
package com.zmysna.corresponse;
import java.util.LinkedList;
import java.util.Random;
/*
生产者
*/
public class LockProducer implements Runnable {
private LinkedList<Integer> queue;
private int length;
public LockProducer(LinkedList<Integer> queue, int length) {
this.queue = queue;
this.length = length;
}
@Override
public void run() {
try {
while (true) {
Random rd = new Random();
Test02.lock.lock();
if (queue.size() == length) {
Test02.isEmpty.signalAll();
Test02.isFull.await();
}
int data = rd.nextInt(100);
System.out.println(Thread.currentThread().getName() + "开始生产数字" + data);
queue.addLast(data);
Thread.sleep(1000);
Test02.lock.unlock();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.zmysna.corresponse;
import java.util.LinkedList;
/*
消费者
*/
public class LockConsumer implements Runnable {
private LinkedList<Integer> queue;
public LockConsumer(LinkedList<Integer> queue) {
this.queue = queue;
}
@Override
public void run() {
try {
while (true) {
Test02.lock.lock();
if (queue.isEmpty()) {
Test02.isFull.signalAll();
Test02.isEmpty.await();
}
else System.out.println(Thread.currentThread().getName() + "消费了" + queue.removeFirst());
Test02.lock.unlock();
Thread.sleep(2000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.zmysna.corresponse;
import java.util.LinkedList;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Test02 {
static Lock lock = new ReentrantLock();
static Condition isFull = lock.newCondition();
static Condition isEmpty = lock.newCondition();
public static void main(String[] args) {
LinkedList<Integer> queue = new LinkedList<>();
LockProducer producer01 = new LockProducer(queue,2);
LockConsumer comsumer01 = new LockConsumer(queue);
new Thread(producer01, "生产者01:").start();
new Thread(comsumer01, "消费者03:").start();
new Thread(comsumer01, "消费者04:").start();
}
}