zoukankan      html  css  js  c++  java
  • JAVA JUC 用同步锁解决生产者消费者问题

    JAVA多线程的形式比较独特,用的还是少了...

    class Clerk{
        private int product;
        private ReentrantLock lock = new ReentrantLock();
        private Condition con = lock.newCondition();
    
        public void get(){
            lock.lock();
    
            try {
                if(product<20){
                    product++;
                    System.out.println(Thread.currentThread().getName()+"生产一件商品");
                    con.signalAll();
                }else{
                    System.out.println("仓库已满");
                    try {
                        con.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
    
        }
        public void sale(){
            lock.lock();
            try {
                if(product>0){
                    System.out.println(Thread.currentThread().getName()+"消费一件商品");
                    product--;
                    con.signalAll();
                }else{
                    System.out.println(Thread.currentThread().getName()+"没有商品");
                    try {
                        con.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
    
                }
            } finally {
                lock.unlock();
            }
        }
    }
    class prductor implements Runnable{
        private Clerk c ;
        prductor(Clerk c){
            this.c=c;
        }
        @Override
        public void run() {
            while (true){
                c.get();
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    
        }
    }
    class cus implements Runnable{
        private Clerk c ;
        cus(Clerk c){
            this.c=c;
        }
        @Override
        public void run() {
            while (true){
                c.sale();
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    
        }
    }
    public class ProCusProblem{
        public static void main(String[] args) {
            Clerk c = new Clerk();
            prductor p = new prductor(c);
            cus cs = new cus(c);
            new Thread(p,"生产者1").start();
            new Thread(p,"生产者2").start();
            new Thread(p,"生产者3").start();
            new Thread(cs,"消费者1").start();
    
        }
    
    
    }
  • 相关阅读:
    HTML+CSS基础
    学习C++——实践者的方法(转整)
    招聘要求看技术发展
    笔记本自建无线wifi
    sortAlgorithms
    耐得住寂寞,拥得了繁华
    C++函数重载实现原理浅析
    程序员指南
    tf.image.non_max_suppression()
    函数 不定长参数
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/12469048.html
Copyright © 2011-2022 走看看