zoukankan      html  css  js  c++  java
  • 多线程:简易版本生产消费者模式纯语言概述

    一个消费者,一个生产者,一共就两个线程

    首先用最简单的语言描述一下这个模型
    对象为 资源对象,资源对象包括了生产方法,和消费方法,以及计数器
    生产者对象,只会生产
    消费者对象,只会销售
    生产者、消费者实现了Runnable接口,同时拥有一个资源字段和为资源字段构造函数
    主函数New了一个资源对象
    New了一个生产者,消费者分别放入一个Thread中执行,结果很可能生产者执行一半,消费者就来执行了,为了避免这种情况为共享数据部分加入Synchronized关键字,进行同步处理
    但是还有一个问题,生产者,消费者,何时运行的时机并没有确定,我们希望的是生产者生产一个,消费者消费一个。
    这个时候有了实现的思路
    添加一个判定Boolean标记盘子里是否有东西
    首先要判断盘子里是否有东西,如果有,生产者线程暂停,
    消费者进行消费,消费完毕后,唤醒生产者,修改标记为有东西。
    如果没有东西生产者进行生产的时候消费,生产完毕唤醒消费者。

    package hysy32.MultiThread;
    
    
    /**
     * Created by 小管 on 2017/4/4.
     */
    public class ShengChanXiaofeiLockDemo {
        public static void main(String[] args){
            mResource m = new mResource();
            shengchanzhe scz = new shengchanzhe(m);
            xiaofeizhe xfz = new xiaofeizhe(m);
            Thread t1 = new Thread(scz);
            Thread t2 = new Thread(xfz);
            t1.start();
            t2.start();
    
        }
    }
    
    class mResource {
    //    Lock l = new ReentrantLock();//声明一个对象锁定
        public boolean isEmpty=true;//标记盘子是否为空
    
        private int count=0;//计数器
    
    
        public synchronized void shengchan() throws InterruptedException {
    
    
    
                if (isEmpty) {/*如果没有对象则进入生产模式*/
    
                    count += 1;//生产+1
                    System.out.println("我生产了第:" + count + "件商品");//报告生产了产品
                    isEmpty=false;//改变盘子
                    notify();
                    //唤醒消费者
    
                } else {
                    wait();//进入等待状态
                }
    
    
        }
        public synchronized void xiaofei() throws InterruptedException {
    
    
    
                if (!isEmpty) {//如果盘子非空则进入消费
                    //报告消费状态
                    System.out.println("我消费了第:" + count + "件商品");
                    //改变盘子状态
                    isEmpty = true;
                    notify();//唤醒生产者
        } else {
                    wait();//如果盘子是空的则休眠
    
        }
    
    
        }
    
    }
    
    class shengchanzhe implements Runnable {
        private mResource r;
    
        public shengchanzhe(mResource r) {
            this.r = r;
        }
    
        @Override
        public void run() {
            while (true) {
                try {
                    r.shengchan();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    
    
        }
    }
    
    class xiaofeizhe implements Runnable {
        private mResource r;
    
        xiaofeizhe(mResource r) {
            this.r = r;
        }
    
        @Override
        public void run() {
            while (true) {
                try {
                    r.xiaofei();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

      

      

  • 相关阅读:
    nginx能访问html静态文件但无法访问php文件
    LeetCode "498. Diagonal Traverse"
    LeetCode "Teemo Attacking"
    LeetCode "501. Find Mode in Binary Search Tree"
    LeetCode "483. Smallest Good Base" !!
    LeetCode "467. Unique Substrings in Wraparound String" !!
    LeetCode "437. Path Sum III"
    LeetCode "454. 4Sum II"
    LeetCode "445. Add Two Numbers II"
    LeetCode "486. Predict the Winner" !!
  • 原文地址:https://www.cnblogs.com/hysys32/p/6667030.html
Copyright © 2011-2022 走看看