生產者消費者java示例-wait()/notify()

package com.indi.wzl.Lock;

import java.util.ArrayList;
import java.util.List;

/**
 * 生產者-消費者模型, wait()/notify()
 * @Auther: zonglin_wu
 * @Date: 2020/2/5 16:54
 * @Description:
 */
public class ProducerConsumer {

    /**
     * 自定義堆棧
     */
    public class Mystack {
        public List<String> list = new ArrayList<>();

        /**
         * 生產
         * @throws InterruptedException
         */
        public synchronized void push() throws InterruptedException {
            String name = Thread.currentThread().getName();
            if (list.size() == 10) {
                this.wait();
                System.out.println(name+":notify");
            }
            list.add("1");
            System.out.println(name+": push=" + list.size());
            this.notifyAll();
            this.wait();
        }

        /**
         * 消費
         * @return
         */
        public synchronized String pop(){
            String name = Thread.currentThread().getName();
            while (list.size() == 0) {
                //沒有東西消費就休眠,此處要用while不用if,因爲消費者可能喚醒消費者
                try {
                    System.out.println(name + ":wait");
                    this.wait();
                    System.out.println(name+":notify");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            String value = list.get(0);
            System.out.println(name + ":getValue:" + value + ",size:" + list.size());
            list.remove(0);
            this.notifyAll();
            return value;
        }


    }

    /**
     * 生產者
     */
    public class ProducerTest extends Thread {
        public Mystack mystack;

        public ProducerTest(Mystack mystack) {
            this.mystack = mystack;
        }

        @Override
        public void run() {
            int i = 0;
            while (true){
                /*try {
                    Thread.currentThread().sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }*/
                try {
                    mystack.push();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                i++;
                if (i == 100){
                    break;
                }
            }
        }

    }

    /**
     * 消費者
     */
    public class ConsumerTest extends Thread {

        public Mystack mystack;

        public ConsumerTest(Mystack mystack) {
            this.mystack = mystack;
        }

        @Override
        public void run() {
            while (true){
                mystack.pop();
            }
        }

    }

    public static void main(String[] args) {
        ProducerConsumer test = new ProducerConsumer();
        Mystack mystack = test.new Mystack();
        ProducerTest producerTest1 = test.new ProducerTest(mystack);
        ProducerTest producerTest2 = test.new ProducerTest(mystack);
        ConsumerTest consumerTest1 = test.new ConsumerTest(mystack);
        ConsumerTest consumerTest2 = test.new ConsumerTest(mystack);
        ConsumerTest consumerTest3 = test.new ConsumerTest(mystack);
        ConsumerTest consumerTest4 = test.new ConsumerTest(mystack);
        producerTest1.setName("p1");
        producerTest2.setName("p2");
        consumerTest1.setName("c1");
        consumerTest2.setName("c2");
        consumerTest3.setName("c3");
        consumerTest4.setName("c4");
        producerTest1.start();
        producerTest2.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        consumerTest1.start();
        consumerTest2.start();
        consumerTest3.start();
        consumerTest4.start();
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章