请用wait-notify写一段代码来解决生产者-消费者问题。
生产者-消费者问题是一个经典的并发问题,它描述的是两类并发操作的问题:生产者将数据放入缓冲区,消费者从缓冲区取出数据。使用wait()和notify()方法可以在Java中实现这个问题的解决方案。
以下是一个简单的示例,其中包含一个生产者线程和一个消费者线程,他们共享一个固定大小的缓冲区:
public class ProducerConsumerDemo {
public static void main(String[] args) { SharedBuffer sharedBuffer = new SharedBuffer(10);
Thread producer = new Thread(new Producer(sharedBuffer)); Thread consumer = new Thread(new Consumer(sharedBuffer));
producer.start(); consumer.start(); }
}
class SharedBuffer { private final int maxSize; private final Queue<Integer> buffer = new LinkedList<>();
public SharedBuffer(int maxSize) { this.maxSize = maxSize; }
public synchronized void produce(int item) throws InterruptedException { while (buffer.size() == maxSize) { wait(); } buffer.add(item); notifyAll(); }
public synchronized int consume() throws InterruptedException { while (buffer.isEmpty()) { wait(); } int item = buffer.poll(); notifyAll(); return item; }
}
class Producer implements Runnable { private final SharedBuffer sharedBuffer;
public Producer(SharedBuffer sharedBuffer) { this.sharedBuffer = sharedBuffer; }
@Override public void run() { for (int i = 0; i < 20; i++) { try { sharedBuffer.produce(i); System.out.println("Produced: " + i); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }
}
class Consumer implements Runnable { private final SharedBuffer sharedBuffer;
public Consumer(SharedBuffer sharedBuffer) { this.sharedBuffer = sharedBuffer; }
@Override public void run() { for (int i = 0; i < 20; i++) { try { int item = sharedBuffer.consume(); System.out.println("Consumed: " + item); Thread.sleep(1500); } catch (InterruptedException e) { e.printStackTrace(); } } }
}
这个示例中,SharedBuffer类提供了produce和consume方法来分别添加和移除缓冲区中的元素。这两个方法都是synchronized的,以确保在同一时间只有一个线程可以访问它们。当缓冲区满时,生产者线程会调用wait()方法进入等待状态,直到消费者线程消费了一些元素并调用notifyAll()方法来唤醒它。同样,当缓冲区为空时,消费者线程会等待,直到生产者线程生产了一些元素。