zoukankan      html  css  js  c++  java
  • 经典多线程问题(二)-生产者消费者问题

    /**
     * @ClassName Question09
     * @Description: 经典生产者消费者问题
     * @Author xtanb
     * @Date 2019/10/21
     * @Version V1.0
     **/
    public class Question09 {
        class CubbyHole{
            private int seq;
            private boolean able = false;
    
            public synchronized int get(){
                while(!able){
                    try {
                        wait();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                able=false;
                notify();
                return seq;
            }
    
            public synchronized void put(int value){
                while(able){
                    try {
                        wait();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                seq =value;
                able = true;
                notify();
            }
        }
    
        class Producer extends Thread{
            private CubbyHole cubbyHole;
            private int number;
    
            public Producer(CubbyHole c,int number){
                cubbyHole= c;
                this.number=number;
            }
            public void run(){
                for(int i=0;i<10;i++){
                    cubbyHole.put(i);
                    System.out.println("Productor"+number +" put "+i);
                    try {
                        sleep((int)Math.random()*100);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        class Consumer extends Thread{
            private CubbyHole cubbyHole;
            private int number;
    
            public Consumer(CubbyHole cubbyHole, int number) {
                super();
                this.cubbyHole = cubbyHole;
                this.number = number;
            }
    
            public void run(){
                int value =0;
                for(int i=0;i<10;i++){
                    value= cubbyHole.get();
                    System.out.println("Customer"+number+" get "+value );
                }
            }
        }
        public static void main(String[] args) {
            CubbyHole cubbyHole = new Question09().new CubbyHole();
            Producer p1 =new Question09().new Producer(cubbyHole, 1);
            Consumer c1 =new Question09().new Consumer(cubbyHole, 1);
            p1.start();
            c1.start();
        }
    }

     2.0 reentranlock

    package com.example.demo.study.questions;
    
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    /**
     * @ClassName Question15
     * @Description: 生产者消费者问题
     * @Author xtanb
     * @Date 2019/10/22
     * @Version V1.0
     **/
    class CubbyHoles{
        private volatile boolean flag = true;
        private Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        private int num = 1;
    
        public void produce(){
            try{
                lock.lock();
                while (!flag){
                    condition.await();
                }
                System.out.println("生产"+num);
                flag = false;
                condition.signal();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }
    
        public void consumer(){
            try{
                lock.lock();
                while (flag){
                    try{
                        condition.await();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                System.out.println("消费"+num);
                num++;
                flag = true;
                condition.signal();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }
    }
    class Producers extends Thread{
        private CubbyHoles cubbyHole;
        private int number;
    
        public Producers(CubbyHoles c,int number){
            cubbyHole= c;
            this.number=number;
        }
        public void run(){
            for(int i=0;i<10;i++){
                cubbyHole.produce();
                try {
                    sleep((int)Math.random()*100);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    class Consumers extends Thread{
        private CubbyHoles cubbyHole;
        private int number;
    
        public Consumers(CubbyHoles cubbyHole, int number) {
            super();
            this.cubbyHole = cubbyHole;
            this.number = number;
        }
    
        public void run(){
            for(int i=0;i<10;i++){
                cubbyHole.consumer();
            }
        }
    }
    
    public class Question15 {
        public static void main(String[] args) {
            CubbyHoles cubbyHoles = new CubbyHoles();
            new Producers(cubbyHoles,1).start();
            new Consumers(cubbyHoles,1).start();
        }
    }
  • 相关阅读:
    2、词法分析--4、字面值--2、字符串拼接
    2、词法分析--3、标识符和关键字
    2、词法分析-- 1、行结构
    git本机服务器配置(四):git+TortoiseGit+gitblit配置本机服务器
    git本机服务器配置(三):Gitblit的安装
    git本机服务器配置(二):TortoiseGit的安装
    git本机服务器配置(一):git的安装
    python 中 dlib库的安装
    正向代理和方向代理的区别和使用
    php应用路径变量问题总结
  • 原文地址:https://www.cnblogs.com/helloworldmybokeyuan/p/11714948.html
Copyright © 2011-2022 走看看