• 生产消费_lock和阻塞队列


    lock

    1. 这里的lock只需要一把锁
      因为同时还要配合状态 一起决定
    2. 一定要在try里面用 一定要unlock
    public class Test {
        public static void main(String[] args) {
            //传统版本
            AirConditional airConditional = new AirConditional();
            new Thread(()->{
                for (int i = 0; i < 5; i++) {
                    airConditional.produce();
                }
            }).start();
            new Thread(()->{
                for (int i = 0; i < 5; i++) {
                    airConditional.consume();
                }
            }).start();
        }
    }
    class AirConditional{
        int temp ;
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
    
        public void produce(){
            try {
                lock.lock();
                while( temp != 0){
                    condition.await();
                }
                temp++;
                System.out.println("生产后为"+temp);
                condition.signalAll();
            }catch (Exception e){
    
            }
            finally {
                 lock.unlock();   
            }
        }
        public void consume(){
            try {
                lock.lock();
                while( temp != 1){
                    condition.await();
                }
                temp--;
                System.out.println("消费后为"+temp);
                condition.signalAll();
            }catch (Exception e){
    
            }
            finally {
                lock.unlock();
            }
        }
    }
    

    bq

    但如果不睡的话,会稀巴烂
    本质就是put和take,加定时的话就是offer和poll

    class AirConditional_bq{
        public volatile boolean flag = true;
        private AtomicInteger integer = new AtomicInteger();
        //为什么要string 就是因为解耦-原子类
        private BlockingQueue<String> bq;
    
        public AirConditional_bq(BlockingQueue<String> bq) {
            this.bq = bq;
        }
    
        public void add(){
            while(flag){
                int i = integer.incrementAndGet();
                try {Thread.sleep(1000);
                    bq.put(i+"");
                    System.out.println("生产了"+i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        public void minus(){
            while(flag){
                try {Thread.sleep(1000);
                    String take = bq.take();
                    System.out.println("消费了"+take);
                    integer.decrementAndGet();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        public static void main(String[] args) throws InterruptedException {
            AirConditional_bq conditionalBq = new AirConditional_bq(new ArrayBlockingQueue<>(1));
            new Thread(()->{
                conditionalBq.add();
            }).start();
            new Thread(()->{
                conditionalBq.minus();
            }).start();
            Thread.sleep(2000);
            conditionalBq.flag = false;
            System.out.println("主线程退出");
        }
    }
    
  • 相关阅读:
    LeetCode 560. Subarray Sum Equals K (子数组之和等于K)
    25、LinkedList特有方法
    24、List三个子类的特点
    23、数据结构之数组和链表
    22、Vector简介
    21、List遍历时修改元素的问题
    20、List集合中特有的方法
    19、集合概述
    18、Random类简介
    17、enum简介
  • 原文地址:https://www.cnblogs.com/purexww/p/15250442.html
Copyright © 2020-2023  润新知