多线程之生产者消费者模式扩展

多线程生产者消费者模式是并发编程中一个经典的问题,它涉及到线程同步和资源竞争的问题。在Java中,我们可以使用wait()notify()notifyAll()方法来实现线程间的通信。 以下是生产者消费者模式的扩展示例,其中我们将使用阻塞队列(BlockingQueue)来简化生产者和消费者之间的交互。

代码示例

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
class Producer implements Runnable {
    private final BlockingQueue<Integer> queue;
    public Producer(BlockingQueue<Integer> queue) {
        this.queue = queue;
    }
    public void run() {
        try {
            for (int i = 0; i < 10; i++) {
                System.out.println("Produced: " + i);
                queue.put(i);
                Thread.sleep(1000); // 模拟生产耗时
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
class Consumer implements Runnable {
    private final BlockingQueue<Integer> queue;
    public Consumer(BlockingQueue<Integer> queue) {
        this.queue = queue;
    }
    public void run() {
        try {
            while (true) {
                Integer take = queue.take();
                System.out.println("Consumed: " + take);
                Thread.sleep(1000); // 模拟消费耗时
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
public class ProducerConsumerExample {
    public static void main(String[] args) {
        BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(5); // 设置队列大小为5
        Thread producerThread = new Thread(new Producer(queue));
        Thread consumerThread = new Thread(new Consumer(queue));
        producerThread.start();
        consumerThread.start();
    }
}

运行结果

当运行上述程序时,生产者和消费者将交替执行,生产者将生产数据并将其放入队列中,消费者将消费队列中的数据。以下是可能的输出:

Produced: 0
Produced: 1
Produced: 2
Produced: 3
Produced: 4
Consumed: 0
Consumed: 1
Produced: 5
Consumed: 2
Produced: 6
Consumed: 3
Produced: 7
Consumed: 4
Produced: 8
Consumed: 5
Produced: 9
Consumed: 6
Consumed: 7
Consumed: 8
Consumed: 9

扩展说明

  1. 阻塞队列(BlockingQueue): 在这个例子中,我们使用了LinkedBlockingQueue,它是一个线程安全的队列,可以用来实现生产者消费者模式。它内部实现了put()take()方法,这些方法在队列满或空时会自动阻塞。

  2. 线程同步: 通过BlockingQueue,我们避免了显式地使用wait()notify()notifyAll()方法。BlockingQueue内部已经处理了这些同步细节。

  3. 线程中断: 在InterruptedException捕获块中,我们调用了Thread.currentThread().interrupt()来恢复中断状态,这是因为InterruptedException会清除中断状态。

  4. 扩展点: 你可以根据需要扩展这个模式,例如,增加多个生产者和消费者,使用不同的BlockingQueue实现(如ArrayBlockingQueuePriorityBlockingQueue),或者添加更多的业务逻辑。

总结

这个例子展示了如何在Java中使用BlockingQueue实现生产者消费者模式。通过使用BlockingQueue,我们可以更容易地处理多线程同步问题,并简化代码。在实际应用中,这个模式可以根据具体需求进行相应的扩展和优化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值