生产者生成0-10的数字,消费者拿到后打印

import java.util.Queue;
import java.util.LinkedList;

public class ProducerConsumerExample {

    // 共享队列
    private static final Queue<Integer> queue = new LinkedList<>();
    private static final int LIMIT = 10;

    // 生产者线程
    static class Producer implements Runnable {
        @Override
        public void run() {
            int value = 0;
            while (true) {
                synchronized (queue) {
                    while (queue.size() == LIMIT) {
                        try {
                            System.out.println("Queue is full, producer is waiting");
                            queue.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    queue.add(value++);
                    System.out.println("Produced: " + value);
                    queue.notifyAll();
                }
                try {
                    Thread.sleep((int)(Math.random() * 1000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 消费者线程
    static class Consumer implements Runnable {
        @Override
        public void run() {
            while (true) {
                synchronized (queue) {
                    while (queue.isEmpty()) {
                        try {
                            System.out.println("Queue is empty, consumer is waiting");
                            queue.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    int value = queue.poll();
                    System.out.println("Consumed: " + value);
                    queue.notifyAll();
                }
                try {
                    Thread.sleep((int)(Math.random() * 1000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        Thread producerThread = new Thread(new Producer());
        Thread consumerThread = new Thread(new Consumer());

        producerThread.start();
        consumerThread.start();
    }
}

代码解释

  1. 共享队列

    • 使用 Queue 作为共享数据结构,存储生产者生成的数字。

    • LIMIT 定义了队列的最大容量。

  2. 生产者线程

    • 生产者线程在 run 方法中生成数字并将其添加到队列中。

    • 如果队列已满,生产者线程会调用 wait() 方法等待,直到队列中有空间。

    • 每次生产一个数字后,调用 notifyAll() 方法唤醒等待的消费者线程。

  3. 消费者线程

    • 消费者线程在 run 方法中从队列中取出数字并打印。

    • 如果队列为空,消费者线程会调用 wait() 方法等待,直到队列中有数据。

    • 每次消费一个数字后,调用 notifyAll() 方法唤醒等待的生产者线程。

  4. 同步机制

    • 使用 synchronized 块确保对共享队列的访问是线程安全的。

    • 使用 wait()notifyAll() 方法实现线程间的同步。

注意事项

  • 线程安全:使用 synchronized 块确保对共享队列的访问是线程安全的。

  • 等待和通知:使用 wait()notifyAll() 方法实现线程间的同步。

  • 随机延迟:在生产者和消费者线程中添加随机延迟,模拟实际的生产消费时间。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kkoneone11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值