Java生产者消费者,控制一个数在0-4之间变化

代码

public class Main {

    private static int i = 0;
    
    public static void main(String[] args) {
        Main main = new Main();
        producer(main);
        consumer(main);
    }

    private static void consumer(Main main) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    synchronized (main) {
                        while (i > 0) {
                            System.out.println("消费: " + i);
                            i--;
                        }
                        try {
                            main.notifyAll();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }

    private static void producer(Main main) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    synchronized (main) {
                        while (i < 4) {
                            i++;
                            System.out.println("生产: " + i);
                        }
                        try {
                            main.wait();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }
}
  • 使用wait() ,notify(),notifyAll()需要使用synchronized()进行同步
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Java语言实现的多线程生产者-消费者问题的示例代码: ```java import java.util.Random; import java.util.concurrent.Semaphore; public class ProducerConsumer { private static final int BUFFER_SIZE = 10; // 缓冲池大小 private static final int NUM_PRODUCERS = 5; // 生产者数量 private static final int NUM_CONSUMERS = 5; // 消费者数量 private static final int MAX_ITEM = 100; // 产品最大值 private static int[] buffer = new int[BUFFER_SIZE]; // 缓冲池 private static Semaphore emptySema = new Semaphore(BUFFER_SIZE); // 空闲缓冲块信号量 private static Semaphore fullSema = new Semaphore(0); // 满缓冲块信号量 private static Semaphore putMutex = new Semaphore(1); // 生产者信号量 private static Semaphore getMutex = new Semaphore(1); // 消费者信号量 public static void main(String[] args) { Thread[] producers = new Thread[NUM_PRODUCERS]; Thread[] consumers = new Thread[NUM_CONSUMERS]; for (int i = 0; i < NUM_PRODUCERS; i++) { producers[i] = new Producer(i); producers[i].start(); } for (int i = 0; i < NUM_CONSUMERS; i++) { consumers[i] = new Consumer(i); consumers[i].start(); } try { for (int i = 0; i < NUM_PRODUCERS; i++) { producers[i].join(); } for (int i = 0; i < NUM_CONSUMERS; i++) { consumers[i].join(); } } catch (InterruptedException e) { e.printStackTrace(); } } static class Producer extends Thread { private int id; public Producer(int id) { this.id = id; } public void run() { while (true) { try { Thread.sleep(new Random().nextInt(1000)); // 随机等待一段时间 emptySema.acquire(); // 获取空闲缓冲块 putMutex.acquire(); // 获取生产者信号量 int index = putItem(); // 生产产品并放入缓冲池 putMutex.release(); // 释放生产者信号量 fullSema.release(); // 增加满缓冲块计数 System.out.printf("生产者%d在缓冲块%d中生产了%d\n", id, index, buffer[index]); } catch (InterruptedException e) { e.printStackTrace(); } } } private int putItem() { int item = new Random().nextInt(MAX_ITEM); for (int i = 0; i < BUFFER_SIZE; i++) { if (buffer[i] == 0) { buffer[i] = item; return i; } } return -1; } } static class Consumer extends Thread { private int id; public Consumer(int id) { this.id = id; } public void run() { while (true) { try { Thread.sleep(new Random().nextInt(1000)); // 随机等待一段时间 fullSema.acquire(); // 获取满缓冲块 getMutex.acquire(); // 获取消费者信号量 int index = getItem(); // 从缓冲池中消费产品 getMutex.release(); // 释放消费者信号量 emptySema.release(); // 增加空闲缓冲块计数 System.out.printf("消费者%d从缓冲块%d中取出了%d\n", id, index, buffer[index]); } catch (InterruptedException e) { e.printStackTrace(); } } } private int getItem() { for (int i = 0; i < BUFFER_SIZE; i++) { if (buffer[i] != 0) { int item = buffer[i]; buffer[i] = 0; return i; } } return -1; } } } ``` 在这个示例代码中,我们使用了`Semaphore`来创建信号量。`emptySema`表示空闲缓冲块的数量,`fullSema`表示满缓冲块的数量,`putMutex`和`getMutex`分别是生产者和消费者的信号量。在生产者和消费者的`run`方法中,我们使用`acquire`和`release`方法来获取和释放信号量和缓冲块。在生产者的`putItem`方法中,我们随机生成一个产品并将其放入缓冲池中,然后返回该产品所在的缓冲块的索引。在消费者的`getItem`方法中,我们查找第一个非空缓冲块并从中取出产品,然后将该缓冲块清空,并返回该缓冲块的索引。 运行示例代码,可以看到生产者和消费者在缓冲池中生产和消费产品的过程。由于生产者和消费者是并发运行的,因此可能会出现一些断点和次序的变化。但是,由于我们使用了信号量来进行同步控制,因此缓冲池中的产品数量始终不会超过其容量限制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值