zoukankan      html  css  js  c++  java
  • 生产者消费者(Lock)

    package com.kaibing.design;
    
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class ProductorAndCustomerLock {
    
    
        public static void main(String[] args) {
            Clerk clerk = new Clerk();
    
            Productor productor = new Productor(clerk);
            Customer customer = new Customer(clerk);
    
            new Thread(productor, "生产者A:").start();
            new Thread(customer, "消费者A:").start();
    
            new Thread(productor, "生产者B:").start();
            new Thread(customer, "消费者B:").start();
        }
    
        static class Clerk {//店员类
    
            private int product = 0;
    
            private Lock lock = new ReentrantLock();
            private Condition condition = lock.newCondition();
    
            public  void get() {//进货
    
                lock.lock();//加锁
    
                try{
                    while (product >= 1) {//容量为1
                        System.out.println("库存已满");
                        try {
                            condition.await();//等待:为了避免虚假唤醒,wait在循环中使用
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    //进货
                    System.out.println("库存" + ++product);
                    condition.signalAll();//唤醒
                }finally{
    
                    lock.unlock();//释放锁
                }
    
    
            }
    
            public  void sale() {//卖货
    
                lock.lock();//加锁
    
                try{
                    while (product <= 0) {
                        System.out.println("库存已空");
                        try {
                            condition.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("卖货" + --product);
                    condition.signalAll();
                }finally {
    
                    lock.unlock();//释放锁
    
                }
    
    
            }
        }
    
        static class Productor implements Runnable {
    
            private Clerk clerk;
    
            public Productor(Clerk clerk) {
                this.clerk = clerk;
            }
    
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    clerk.get();//消费货物
                }
            }
        }
    
        static class Customer implements Runnable {
    
            private Clerk clerk;
    
            public Customer(Clerk clerk) {
                this.clerk = clerk;
            }
    
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
    
                    clerk.sale();//消费货物
                }
            }
        }
    
    
    }
  • 相关阅读:
    ISAPI_Rewrite规则写法
    用Windows 2003配置邮件服务器
    如何保护.net中的dll文件(防破解、反编译)
    The Tao Of Programming 编程之道
    Request.ServerVariables (server environment variable)
    Windows下搭建Subversion 服务器
    邮件服务器反解析退信问题分析
    关于公网和内网之间Socket的通信
    .net安装部署中添加卸载程序简单方法
    构建高性能ASP.NET站点 剖析页面处理过程
  • 原文地址:https://www.cnblogs.com/kaibing/p/9258160.html
Copyright © 2011-2022 走看看