• 多线程_多生产者多消费者的实现问题


      多生产者多消费者是Java中的一个经典的案例,采用等待唤醒机制来实现,代码如下:

      

    public class ProducerConsumer {
        public static void main(String[] args) {
            Resource res=new Resource();
            Producer pro1=new Producer(res);
            Producer pro2=new Producer(res);
            Consumer con1=new Consumer(res);
            Consumer con2=new Consumer(res);
            Thread t1=new Thread(pro1);
            Thread t2=new Thread(pro2);
            Thread t3=new Thread(con1);
            Thread t4=new Thread(con2);
            t1.start();
            t2.start();
            t3.start();
            t4.start();
        }
    }
    class Resource {
        private int count;
        private boolean flag=true;
        public synchronized void produce()  {
            while(!flag) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName()+"生产"+count+"烤鸭");
            count++;
            flag=false;
            this.notifyAll();
        }
        public synchronized void consume() {
            while(flag) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName()+"消费"+count+"烤鸭");
            flag=true;
            this.notifyAll();
        }
    }
    class Producer implements Runnable{
        Resource r;
        
        public Producer(Resource r) {
            super();
            this.r = r;
        }
    
        @Override
        public void run() {
            while(true) {
                r.produce();
            }
        }
        
    }
    class Consumer implements Runnable {
        Resource r;
        public Consumer(Resource r) {
            super();
            this.r = r;
        }
        @Override
        public void run() {
            while(true) {
                r.consume();
            }
        }
        
    }

      还有一种采用BlockQueue实现的多生产者多消费者模式:

    import java.util.concurrent.ArrayBlockingQueue;
    
    /**
     * Created by Administrator on 2016/6/30.
     */
    public class Demo {
        public static void main(String[] args) {
            MyBuffer buffer=new MyBuffer();
            Producer pro1=new Producer(buffer);
            Producer pro2=new Producer(buffer);
            Consumer con1=new Consumer(buffer);
            Consumer con2=new Consumer(buffer);
            pro1.start();
            pro2.start();
            con1.start();
            con2.start();
        }
    }
    class MyBuffer {
        private ArrayBlockingQueue queue=new ArrayBlockingQueue(10);
        Object obj=null;
        public void put(Object obj) {
            try {
                queue.put(obj);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        public Object take() {
            try {
                obj= queue.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                return obj;
            }
        }
    }
    class Producer extends Thread{
        private MyBuffer buffer1=new MyBuffer();
        static int count;
        public Producer(MyBuffer buffer1) {
            this.buffer1 = buffer1;
        }
        public void run() {
            while(true) {
                synchronized (this) {
                    String str = "烤鸭" + count;
                    buffer1.put(str);
                    System.out.println(Thread.currentThread().getName() + "生产" + str);
                    count++;
                }
            }
        }
    }
    class Consumer extends Thread {
        private MyBuffer buffer1=new MyBuffer();
        public Consumer(MyBuffer buffer1) {
            this.buffer1 = buffer1;
        }
        public void run() {
            while(true)
            System.out.println(Thread.currentThread().getName()+"消费"+buffer1.take());
        }
    }
     
  • 相关阅读:
    bzoj1084
    bzoj1088 [SCOI2005]扫雷
    [LUOGU] 1892 团伙
    [普及组] 2017 成绩
    [LUOGU] P2661 信息传递
    [LUOGU] P1339 [USACO09OCT]热浪Heat Wave
    [LUOGU] P1828 香甜的黄油 Sweet Butter
    [模板] 单源最短路径
    [LUOGU] 1717 钓鱼
    [UVA] 704 Colour Hash
  • 原文地址:https://www.cnblogs.com/hlhdidi/p/5629600.html
Copyright © 2020-2023  润新知