一. Synchronized+wait()+notify()
1)生产者
class Producer extends Thread{
private Buffer buffer;
public Producer(Buffer buffer) {
this.buffer = buffer;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
buffer.add(i);
System.out.println("生产者生产的数据:" + i);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
2)消费者
class Consumer extends Thread{
private Buffer buffer;
public Consumer(Buffer buffer) {
this.buffer = buffer;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
int val = 0;
try {
val = buffer.pull();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("消费者消费的数据: " + val);
}
}
}
3)缓存区
class Buffer{
private Queue<Integer> queue = new LinkedList<>();
private int size = 5;
public synchronized int pull() throws InterruptedException {
if (queue.size() == 0){
System.out.println("池子空了没法消费,等待生产者生产。。。");
wait();
}
int val = queue.poll();
notify();
return val;
}
public synchronized void add(int val) throws InterruptedException {
if (queue.size() > size){
System.out.println("池子满了,等待消费者消费。。。");
wait();
}
queue.add(val);
notify();
}
}
4)测试
public class ProducerAndConsumerDemo {
public static void main(String[] args) {
Buffer buffer = new Buffer();
Producer producer = new Producer(buffer);
Consumer consumer = new Consumer(buffer);
producer.start();
consumer.start();
}
}
二、Lock+await()+signal
1)生产者
class ProducerV2 extends Thread{
private BufferPool bufferPool;
public ProducerV2(BufferPool bufferPool) {
this.bufferPool = bufferPool;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
bufferPool.increment(i);
System.out.println(Thread.currentThread().getName()+"生产了一个数据");
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
2)消费者
class ConsumerV2 extends Thread{
private BufferPool bufferPool;
public ConsumerV2(BufferPool bufferPool) {
this.bufferPool = bufferPool;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
bufferPool.decrement();
System.out.println(Thread.currentThread().getName()+"消费了一个数据");
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
3)缓存池
class BufferPool {
private Queue<Integer> queue = new LinkedList<>();
private int size = 5;
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public void increment(int val){
lock.lock();
try {
while (queue.size() == size) {
condition.await();
}
queue.add(val);
condition.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void decrement(){
lock.lock();
try {
while (queue.size() == 0) {
condition.await();
}
queue.poll();
condition.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
4)测试
public class ProducerConsumerDemo{
public static void main(String[] args) {
BufferPool bufferPool = new BufferPool();
ProducerV2 producer = new ProducerV2(bufferPool);
ConsumerV2 consumer = new ConsumerV2(bufferPool);
producer.start();
consumer.start();
}
}