java多线程生产者消费者模型

java多线程生产者消费者模型

利用缓冲区解决:管程法

// 生产者,消费者,产品,缓冲区
public class TestPCDemo {
    public static void main(String[] args) {
        // 定义容器
        SynContainer synContainer = new SynContainer();
        // 生产者线程
        Thread pThread = new Thread(new Producer(synContainer));
        // 消费者线程
        Thread cThread = new Thread(new Consumer(synContainer));

        // 开启线程
        pThread.start();
        cThread.start();
    }
}

// 生产者
class Producer implements Runnable {
    // 容器
    SynContainer container;

    // 构造器
    public Producer(SynContainer container) {
        this.container = container;
    }

    // 生产方法
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            container.addProduction(new Production(i));
            System.out.println("生产了第" + i + "个产品");
        }

    }
}

// 消费者
class Consumer implements Runnable {
    // 容器
    SynContainer container;

    // 构造器
    public Consumer(SynContainer container) {
        this.container = container;
    }

    // 消费方法
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("消费了第" + container.popProduction().id + "个产品");
        }
    }
}

// 产品
class Production {
    // 编号
    public int id;

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

// 缓冲区
class SynContainer {
    // 需要一个容器大小
    Production[] productions = new Production[10];
    // 容器计数器
    int count = 0;

    // 生产者放入产品
    public synchronized void addProduction(Production p) {
        // 如果容器满了,需要等待消费者消费
        if (count == productions.length) {
            // 通知消费者,生产等待
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        // 如果没满就生产
        productions[count] = p;
        ++count;
        // 有产品就可以通知消费者消费了
        this.notifyAll();

    }

    // 消费者消费产品
    public synchronized Production popProduction() {
        // 如果容器没有,需要等待生产者生产
        if (count == 0) {
            // 通知生产者,消费等待
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        // 如果有就消费
        count--;
        Production production = productions[count];
        // 通知生产
        this.notifyAll();
        // 看看消费的是什么
        return production;
    }
}

利用标志位解决:红绿灯法

public class TestPCDemo2 {
    public static void main(String[] args) {
        // 定义产品
        Production production = new Production();
        // 两个线程实现类
        Producer producer = new Producer(production);
        Consumer consumer = new Consumer(production);
        // 代理线程并启动
        new Thread(producer).start();
        new Thread(consumer).start();
    }
}

// 生产者
class Producer implements Runnable {
    Production production;

    public Producer(Production production) {
        this.production = production;
    }

    // 生产
    @Override
    public void run() {
        // 生产十个产品
        for (int i = 0; i < 10; i++) {
            this.production.produce("第"+i+"个产品");
        }
    }
}

// 消费者
class Consumer implements Runnable {
    Production production;

    public Consumer(Production production) {
        this.production = production;
    }

    // 消费
    @Override
    public void run() {
        // 消费十个产品
        for (int i = 0; i < 10; i++) {
            this.production.consume();
        }
    }
}

// 产品
class Production {
    // 生产者生产时,消费者等待 true
    // 消费者消费时,生产者等待 false

    // 产品
    public String production;
    // 标志位
    boolean flag = true;

    // 生产者生产
    public synchronized void produce(String production) {
        // 如果flag为false,等待
        if (!flag) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        // 生产
        System.out.println("生产者生产了" + production);
        this.production = production;
        // 通知消费者消费
        this.notifyAll();
        this.flag = !this.flag;
    }

    // 消费者消费
    public synchronized void consume() {
        // 如果flag为true,消费者等待
        if (flag) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("消费者消费了 --> " + production);
        // 通知生产者生产
        this.notifyAll();
        this.flag = !this.flag;
    }
}
posted @ 2022-02-20 14:22  CoderCatIce  阅读(47)  评论(0编辑  收藏  举报