android 生产者 消费者 模式

package com.chenxing.searchjob.sdk.view;

import android.util.Log;

public class TestClass {

    static void msg(String msg){
        Log.i("IMListView", "msg: "+msg);
    }

    public void main(){

        SyncPerson person = new SyncPerson();
        new Thread(new Producer(person, "厂家")).start();
        new Thread(new Consumer(person, "客人")).start();
    }

    public interface Person {

        void consume(String personName) throws InterruptedException;
        void produce(String personName) throws InterruptedException;
    }

    public class Consumer implements Runnable {

        private Person person;
        private String personName;

        public Consumer(Person person, String personName) {
            this.person = person;
            this.personName = personName;
        }

        @Override
        public void run() {
            try {
                person.consume(personName);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public class Producer implements Runnable {

        private Person person;
        private String personName;

        public Producer(Person person, String personName) {
            this.person = person;
            this.personName = personName;
        }

        @Override
        public void run() {

            try {
                person.produce(personName);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static class SyncPerson implements Person{

        private static final String[] lock = {"true"};
        private int bytCount = 0;

        public void produce(String personName) throws InterruptedException {

            synchronized (lock) {
                while (true) {

                    if (bytCount == 5) {

                        lock.notifyAll();
                        msg(personName + "已经生产了足够多的螺丝,等待被消费。。。");
                        Thread.sleep(1000);
                        lock.wait();
                    }
                    bytCount++;
                    msg(personName + "生产了" + bytCount + "只螺丝");
                    Thread.sleep(1000);
                }
            }
        }

        public void consume(String personName) throws InterruptedException {

            synchronized (lock) {
                while (true) {

                    if (bytCount == 0) {

                        lock.notifyAll();
                        msg(personName + "已将螺丝消费完,需要再生产。。。");
                        Thread.sleep(1000);
                        lock.wait();

                    }
                    bytCount--;
                    msg(personName + "消费了" + (5 - bytCount) + "只螺丝");
                    Thread.sleep(1000);
                }
            }
        }
    }
}

发布了187 篇原创文章 · 获赞 65 · 访问量 26万+

猜你喜欢

转载自blog.csdn.net/mhhyoucom/article/details/88763520