qq_978786415 2021-04-20 14:13 采纳率: 0%
浏览 21

ReentrantLock生产者消费者,一般的模型是消费者和生产者一把锁,我想把锁分开,出现了问题。


 
public class ProduceAndCoustomer {
    private static final ReentrantLock lock = new ReentrantLock();
    private static final ReentrantLock lock2 = new ReentrantLock();
    private static Queue<Integer> queue = new LinkedList<>();
    private final static Condition condition1 = lock.newCondition();
    private final static Condition condition3 = lock2.newCondition();
    private static volatile AtomicInteger num = new AtomicInteger(0);
    private static volatile int max = 1000;

    static class Produce1 implements Runnable {
        private String name;

        public Produce1(String name) {
            this.name = name;
        }

        @Override
        public void run() {
            while (true) {
                lock.lock();
                put();
                lock.unlock();
            }
        }
        public void put() {
            while (num.get() == max) {
                try {
                    System.out.println("队列满了,生产者暂停生产");
                    condition1.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            queue.add(1);
            int andIncrement = num.getAndIncrement();
            if (andIncrement < max) {
                condition1.signalAll();
            }
            System.out.println(name + "进行生产,");
            System.out.println("队内总的元素为:" + num.get());
            if (num.get()==max){
                lock2.lock();
                condition3.signalAll();
                lock2.unlock();
            }

        }
    }

    static class Coustomer1 implements Runnable {
        private String name;

        public Coustomer1(String name) {
            this.name = name;
        }

        @Override
        public void run() {
            while (true) {
                lock2.lock();
                take();
                lock2.unlock();
            }
        }

        public int take() {
            int andDecrement = 0;
            int poll = 0;
            try {
                while (num.get() == 0) {
                    System.out.println("队列空了,消费者暂停消费");
                    condition3.await();
                }
                poll = queue.poll();
                andDecrement = num.getAndDecrement();
                if (andDecrement > 1) {
                    condition3.signalAll();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (andDecrement == 0) {
                try {
                    lock.lock();
                    condition1.signalAll();
                    lock.unlock();
                } finally {
                    lock.lock();
                }
            }
            System.out.println(name + "进行消费");
            System.out.println("队内总元素数量为:" + num.get());
            return poll;
        }

    }


    public static void main(String[] args) throws InterruptedException {
        new Thread(new Produce1("生产者1")).start();
        new Thread(new Produce1("生产者2")).start();
        new Thread(new Coustomer1("消费者1")).start();
        new Thread(new Coustomer1("消费者2")).start();
    }


}
  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-07 18:08
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥20 python爬虫遇到空文本取不出来
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络
  • ¥66 关于川崎机器人调速问题
  • ¥15 winFrom界面无法打开
  • ¥30 crossover21 ARM64版本安装软件问题
  • ¥15 mymetaobjecthandler没有进入