实现生产者消费者模式是 Java 多线程编程中的一个重要概念。在多线程环境下,生产者和消费者可以并行执行,提高了程序的效率。这里将详细讲解 Java 多种方式实现生产者消费者模式的完整攻略。
1. 管程法
管程法是最常用的实现生产者消费者模式的方法之一。它要求生产者和消费者共享同一个缓冲区,由缓冲区提供同步的方法供生产者和消费者调用。
以下是管程法的实现示例代码:
public class Buffer {
private int data;
private boolean empty = true;
public synchronized int get() {
while (empty) {
try {
wait();
} catch (InterruptedException e) {}
}
empty = true;
notifyAll();
return data;
}
public synchronized void put(int data) {
while (!empty) {
try {
wait();
} catch (InterruptedException e) {}
}
empty = false;
this.data = data;
notifyAll();
}
}
public class Producer extends Thread {
private Buffer buffer;
public Producer(Buffer buffer) {
this.buffer = buffer;
}
public void run() {
for (int i = 0; i < 10; i++) {
buffer.put(i);
System.out.println("Producer put: " + i);
try {
sleep((int)(Math.random() * 100));
} catch (InterruptedException e) {}
}
}
}
public class Consumer extends Thread {
private Buffer buffer;
public Consumer(Buffer buffer) {
this.buffer = buffer;
}
public void run() {
for (int i = 0; i < 10; i++) {
int data = buffer.get();
System.out.println("Consumer get: " + data);
try {
sleep((int)(Math.random() * 100));
} catch (InterruptedException e) {}
}
}
}
public class Main {
public static void main(String[] args) {
Buffer buffer = new Buffer();
Producer producer = new Producer(buffer);
Consumer consumer = new Consumer(buffer);
producer.start();
consumer.start();
}
}
2. 信号量法
信号量法是另一种实现生产者消费者模式的方法,它使用了信号量机制对临界区资源进行同步控制。
以下是信号量法的实现示例代码:
public class Buffer {
private int data;
private Semaphore mutex = new Semaphore(1);
private Semaphore full = new Semaphore(0);
private Semaphore empty;
public Buffer(int size) {
empty = new Semaphore(size);
}
public void get() throws InterruptedException {
full.acquire();
mutex.acquire();
System.out.println(Thread.currentThread().getName() +
" Consumer get: " + data);
Thread.sleep((int)(Math.random() * 100));
mutex.release();
empty.release();
}
public void put(int data) throws InterruptedException {
empty.acquire();
mutex.acquire();
this.data = data;
System.out.println(Thread.currentThread().getName() +
" Producer put: " + data);
Thread.sleep((int)(Math.random() * 100));
mutex.release();
full.release();
}
}
public class Producer extends Thread {
private Buffer buffer;
public Producer(Buffer buffer) {
this.buffer = buffer;
}
public void run() {
for (int i = 0; i < 10; i++) {
try {
buffer.put(i);
} catch (InterruptedException e) {}
}
}
}
public class Consumer extends Thread {
private Buffer buffer;
public Consumer(Buffer buffer) {
this.buffer = buffer;
}
public void run() {
for (int i = 0; i < 10; i++) {
try {
buffer.get();
} catch (InterruptedException e) {}
}
}
}
public class Main {
public static void main(String[] args) {
Buffer buffer = new Buffer(5);
Producer producer = new Producer(buffer);
Consumer consumer = new Consumer(buffer);
producer.start();
consumer.start();
}
}
以上代码演示了信号量法的实现过程,在实现时,我们需要使用 Semaphore 类创建互斥量和信号量对象,调用其 acquire() 和 release() 方法实现同步控制和线程阻塞。
综上所述,通过管程法和信号量法我们可以实现 Java 的生产者消费者模式。我们只需要根据自己的需求合理选择适合的方法即可。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java多种方式实现生产者消费者模式 - Python技术站