生产者消费者模式

1,wait()和notify()方式的生产消费模式

package com.ydw.dotacatch.Test;

import java.util.PriorityQueue;

public class ProductConsumer {

    private PriorityQueue queue = new PriorityQueue<>(50);

    public static void main(String[] args) {
        ProductConsumer pc = new ProductConsumer();
        new Thread(pc.new Comsumer()).start();
        new Thread(pc.new Product()).start();
    }

    class Product implements Runnable {
        @Override
        public void run() {
            synchronized (queue) {
            while (true) {
                    if (queue.size() >= 50) {
                        System.out.println("停止生产,等待消费");
                        try {
                            Thread.sleep(100);
                            queue.wait();
                        } catch (InterruptedException e) {
                            queue.notify();
                            e.printStackTrace();
                        }
                    } else {
                        queue.offer(1);
                        System.out.println("开始生产" + queue.size());
                        queue.notify();
                    }
                }
            }
        }
    }

    class Comsumer implements Runnable {
        @Override
        public void run() {
            synchronized (queue) {
            while (true) {
                    if (queue.size() > 0) {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        queue.poll();
                        System.out.println("开始消费"  + queue.size());
                        queue.notify();
                    } else {
                        System.out.println("等待生产");
                        try {
                            queue.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}

2,消息队列的方式

package com.ydw.dotacatch.Test;

import java.util.*;
import java.util.PriorityQueue;
import java.util.concurrent.ArrayBlockingQueue;

public class ProductConsumer2 {

    private ArrayBlockingQueue list = new ArrayBlockingQueue<>(10);

    class Product implements Runnable{
        @Override
        public void run() {
                while (true) {
                    if(list.size() < 10) {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("开始生产" + list.size());
                        list.offer(1);
                    }
            }
        }
    }

    class consumer implements Runnable{

        @Override
        public void run() {
                while (true) {
                    if(list.size() > 0) {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("开始消费" + list.size());
                        list.poll();
                    }
            }
        }
    }

    public static void main(String[] args) {
        ProductConsumer2 pc = new ProductConsumer2();
        new Thread(pc.new Product()).start();
        new Thread(pc.new consumer()).start();


    }

}




你可能感兴趣的:(生产者消费者模式)