zoukankan      html  css  js  c++  java
  • java-Runnable加锁实现生产者和消费者的多线程问题

    案例:

    有一家商品售卖机构,只有一名生产者,两名消费者,请采用多线程的方式对这个案例进行实现。

    //库存函数,保存着库存的信息Storage.java
    
    public class Storage {
        //模拟库存
        public Integer num=1;
    }
    
    
    //生产者函数 product.java
    /************************************************************   
    Copyright (C), 1988-1999, Huawei Tech. Co., Ltd.   
    FileName: Customer.java 
    Author:     Light   
    Version :    version1.0      
    Date:       2018/7/11
    Description:    生产者进行生产,生产者需要在消费者进行消费后生产,模拟的内存只有一块,
                    当没有消费时,会提醒消费者消费,唤醒消费者进程 // 模块描述         
    Version:         1.0// 版本信息 
      
    Function List:     // 主要函数及其功能     
    1.
     History: 
      // 历史修改记录 
          
        <author>  <time>   <version >   <desc>       
          Light    2018/7/11     1.0     build this moudle   
    
    ***********************************************************/
    public class Product implements Runnable{
        private Storage storage;
        
        public Product(Storage storage) {
            super();
            this.storage = storage;
        }
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while(true){
                synchronized (storage) {
                    if(this.storage.num<1){
                        //模拟生产
                        this.storage.num++;
                        System.out.println("生产一个");
                        //唤醒队列中所有进程
                        this.storage.notifyAll();
                    }else{
                        try {
                            //进入等待队列
                            this.storage.wait();
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    
    }
    
    //消费者函数 Customer.java
    /************************************************************   
    Copyright (C), 1988-1999, Huawei Tech. Co., Ltd.   
    FileName: Customer.java 
    Author:     Light   
    Version :    version1.0      
    Date:       2018/7/11
    Description:    消费者进行消费,消费者需要在生产者进行生产再进行消费,
                    当没有生产时,会提醒生产者生产,唤醒生产者进程 // 模块描述         
    Version:         1.0// 版本信息 
      
    Function List:     // 主要函数及其功能     
    1.
     History: 
      // 历史修改记录 
          
        <author>  <time>   <version >   <desc>       
          Light    2018/7/11     1.0     build this moudle   
    
    ***********************************************************/
    public class Customer implements Runnable {
        private Storage storage;
        
        public Customer(Storage storage) {
            super();
            this.storage = storage;
        }
        /** 
          * 重写run()
          * @author     [Light](必须) 
          * @see      [run()](可选)  
          * @since     [version 1.0] (必须)  
          */ 
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while(true){
                //加锁
                synchronized (storage) {
                    //判断是否生产者进行了生产
                    if(this.storage.num>=1){
                        //模拟消费
                        this.storage.num--;
                        System.out.println("消费一个");
                        //唤醒队列所有进程
                        this.storage.notifyAll();
                    }else{
                        try {
                            //等待
                            this.storage.wait();
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    
    }
    
    //主函数 MyTest.java
    /************************************************************   
    Copyright (C), 1988-1999, Huawei Tech. Co., Ltd.   
    FileName: MyTest.java 
    Author:     Light   
    Version :    version1.0      
    Date:       2018/7/11
    Description:   消费生产进程模拟 // 模块描述         
    Version:         1.0// 版本信息 
      
    Function List:     // 主要函数及其功能     
    1.
     History: 
      // 历史修改记录 
          
        <author>  <time>   <version >   <desc>       
          Light    2018/7/11     1.0     build this moudle   
    
    ***********************************************************/
    public class MyTest {
    
        public static void main(String[] args) {
         //库存      Storage storage
    =new Storage();
         //生产者 Product p1
    =new Product(storage);
         //两名消费者 Customer c1
    =new Customer(storage); Customer c2=new Customer(storage); Thread t1=new Thread(p1); Thread t2=new Thread(c1); Thread t3=new Thread(c2); t1.start(); t2.start(); t3.start();   } }

    最终执行结果:

    通过对进程的上锁和唤醒,最终实现了生产者和消费者的案例。

  • 相关阅读:
    14. D3D光照
    17. 增加对硬件光照的支持
    ECommerce Starter Kit 数据库表和存储过程一览
    使用 DataAdapter 执行批量更新
    学习Professional ASP.NET 2.0(四)
    c#泛型学习(二)
    学习Professional ASP.NET 2.0(一)
    学习Professional ASP.NET 2.0(二)
    下载:微软网页设计工具CTP测试版(支持asp.net2.0)
    学习ECommerce Starter Kit (1)
  • 原文地址:https://www.cnblogs.com/suifengye/p/9297403.html
Copyright © 2011-2022 走看看