1. 使用wait()和notify()方法:
import java.util.LinkedList;
class ProducerConsumer {
private LinkedList<byte[]> buffer = new LinkedList<>();
private int capacity = 10;
public void produce() throws InterruptedException {
while (true) {
synchronized (this) {
while (buffer.size() == capacity) {
wait();
}
byte[] data = generateData();
buffer.add(data);
System.out.println("Produced: " + data.length + " bytes");
notify();
Thread.sleep(1000);
}
}
}
public void consume() throws InterruptedException {
while (true) {
synchronized (this) {
while (buffer.isEmpty()) {
wait();
}
byte[] data = buffer.removeFirst();
System.out.println("Consumed: " + data.length + " bytes");
notify();
Thread.sleep(1000);
}
}
}
private byte[] generateData() {
// 生成byte数组数据的逻辑
return new byte[10];
}
}
public class Main {
public static void main(String[] args) {
ProducerConsumer pc = new ProducerConsumer();
Thread producerThread = new Thread(() -> {
try {
pc.produce();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread consumerThread = new Thread(() -> {
try {
pc.consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
producerThread.start();
consumerThread.start();
}
}
2. 使用阻塞队列(BlockingQueue):
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
class ProducerConsumer {
private BlockingQueue<byte[]> buffer = new ArrayBlockingQueue<>(10);
public void produce() throws InterruptedException {
while (true) {
byte[] data = generateData();
buffer.put(data);
System.out.println("Produced: " + data.length + " bytes");
Thread.sleep(1000);
}
}
public void consume() throws InterruptedException {
while (true) {
byte[] data = buffer.take();
System.out.println("Consumed: " + data.length + " bytes");
Thread.sleep(1000);
}
}
private byte[] generateData() {
// 生成byte数组数据的逻辑
return new byte[10];
}
}
public class Main {
public static void main(String[] args) {
ProducerConsumer pc = new ProducerConsumer();
Thread producerThread = new Thread(() -> {
try {
pc.produce();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread consumerThread = new Thread(() -> {
try {
pc.consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
producerThread.start();
consumerThread.start();
}
}
3. 使用Lock和Condition:
import java.util.LinkedList;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class ProducerConsumer {
private LinkedList<byte[]> buffer = new LinkedList<>();
private int capacity = 10;
private Lock lock = new ReentrantLock();
private Condition notFull = lock.newCondition();
private Condition notEmpty = lock.newCondition();
public void produce() throws InterruptedException {
while (true) {
lock.lock();
try {
while (buffer.size() == capacity) {
notFull.await();
}
byte[] data = generateData();
buffer.add(data);
System.out.println("Produced: " + data.length + " bytes");
notEmpty.signal();
Thread.sleep(1000);
} finally {
lock.unlock();
}
}
}
public void consume() throws InterruptedException {
while (true) {
lock.lock();
try {
while (buffer.isEmpty()) {
notEmpty.await();
}
byte[] data = buffer.removeFirst();
System.out.println("Consumed: " + data.length + " bytes");
notFull.signal();
Thread.sleep(1000);
} finally {
lock.unlock();
}
}
}
private byte[] generateData() {
// 生成byte数组数据的逻辑
return new byte[10];
}
}
public class Main {
public static void main(String[] args) {
ProducerConsumer pc = new ProducerConsumer();
Thread producerThread = new Thread(()