码迷,mamicode.com
首页 > 编程语言 > 详细

java-Runnable加锁实现生产者和消费者的多线程问题

时间:2018-07-12 01:03:17      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:void   author   exce   todo   ima   技术   huawei   生产   修改   

案例:

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

 

//库存函数,保存着库存的信息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();   } }

最终执行结果:

技术分享图片

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

 

java-Runnable加锁实现生产者和消费者的多线程问题

标签:void   author   exce   todo   ima   技术   huawei   生产   修改   

原文地址:https://www.cnblogs.com/suifengye/p/9297403.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!