天天看點

多線程-線程通信:管程法(生産者和消費者問題)

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;
    }
}