消费者和生产者两种实现

通过阻塞队列实现

package cn.it.cast.dao;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

public class testProduce{
    private final static ArrayBlockingQueue<Apple222> queue= new ArrayBlockingQueue<>(1);
    public static void main(String[] args){
        new Thread(new Producer(queue)).start();
        new Thread(new Producer(queue)).start();
        new Thread(new Consumer(queue)).start();
        new Thread(new Consumer(queue)).start();
    }
}

class Apple222{
    public Apple222(){
    }
}

/**
 * 生产者线程
 */
class Producer implements Runnable{
    private final ArrayBlockingQueue<Apple222> mAbq;
    Producer(ArrayBlockingQueue<Apple222> arrayBlockingQueue){
        this.mAbq = arrayBlockingQueue;
    }

    @Override
    public void run() {
        while (true) {
            Produce();
        }
    }

    private void Produce(){
        try {
            Apple222 apple = new Apple222();
            mAbq.put(apple);
            System.out.println("生产:"+apple);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

/**
 * 消费者线程
 */
class Consumer implements Runnable{

    private ArrayBlockingQueue<Apple222> mAbq;
    Consumer(ArrayBlockingQueue<Apple222> arrayBlockingQueue){
        this.mAbq = arrayBlockingQueue;
    }

    @Override
    public void run() {
        while (true){
            try {
                TimeUnit.MILLISECONDS.sleep(1000);
                comsume();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void comsume() throws InterruptedException {
        Apple222 apple = mAbq.take();
        System.out.println("消费Apple="+apple);
    }
}

通过wait和notifyall实现

public class ProducerCunsumer_Test {
    private static final int MAX_CAPACITY = 10;
    private static List<Object> goods = new ArrayList<Object>();

    public static void main(String[] args) {
        (new ProducerThread()).start();

        (new ConsumerThread()).start();
    }

    static class ProducerThread extends Thread {
        public void run() {
            while (true) {
                // 每隔 1000 毫秒生产一个商品
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }

                synchronized (goods) {
                    // 当前商品满了,生产者等待
                    if (goods.size() == MAX_CAPACITY) {
                        try {
                            System.out.println("Goods full, waiting...");
                            goods.wait();
                        } catch (Exception e) {
                        }
                    }

                    goods.add(new Object());
                    System.out.println("Produce goods, total: " + goods.size());

                    // goods.notify() 也可以
                    goods.notifyAll();
                }
            }
        }
    }

    static class ConsumerThread extends Thread {
        public void run() {
            while (true) {
                // 每隔 500 毫秒消费一个商品
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                }

                synchronized (goods) {
                    // 当前商品空了,消费者等待
                    if (goods.size() == 0) {
                        try {
                            System.out.println("No goods, waiting...");
                            goods.wait();
                        } catch (Exception e) {
                        }
                    }

                    goods.remove(0);
                    System.out.println("Consume goods, total: " + goods.size());

                    // goods.notify() 也可以
                    goods.notifyAll();
                }
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/m0_38054145/article/details/82708379