天天看点

多线程-线程通信:管程法(生产者和消费者问题)

public class ProducerAndCustomer {

    public static void main(String[] args) {
        Buffer buffer = new Buffer();
        new Provider(buffer).start();
        new Customer(buffer).start();
    }

}

/**
 * 生产者
 */
class Provider extends Thread {

    private Buffer buffer;

    public Provider(Buffer buffer) {
        this.buffer = buffer;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            buffer.push(new Product(i));
            System.out.println("=========>生产者生产了第" + (i + 1) + "个产品<=========");
        }
    }
}

/**
 * 消费者
 */
class Customer extends Thread {

    private Buffer buffer;

    public Customer(Buffer buffer) {
        this.buffer = buffer;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("消费了第" + (buffer.pop().getId() + 1) + "个产品");
        }
    }
}

/**
 * 产品数据缓冲区
 */
class Buffer {

    /**
     * 容器大小
     */
    private Product[] products = new Product[10];

    /**
     * 计数器
     */
    private int count;

    /**
     * 生产者
     * @param product
     */
    public synchronized void push(Product product) {
        // 如果容器满了,就需要等待消费者消费
        if (count == products.length) {
            // 通知消费者进行消费,生产者进行等待
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        // 如果容器没有满,就需要丢入添加产品
        products[count] = product;
        count ++;

        // 添加产品之后就可以通知消费者进行消费
        this.notifyAll();
    }

    public synchronized Product pop() {
        // 判断是否可消费
        if (count == 0) {
            // 等待生产者进行生产,消费者进行等待
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        // 消费结束通知其他线程可以进行生产
        this.notifyAll();
        // 如果可以消费
        count --;
        return products[count];
    }

}

/**
 * 产品
 */
class Product {
    private int id;

    public Product(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}