美文网首页
生产者消费者模型

生产者消费者模型

作者: twilight_mao | 来源:发表于2019-01-13 21:03 被阅读0次

三种写法

public class Thread5 {
    private static int num = 0;
    private static int MAX_NUM = 20;
    private static Object lock = new Object();

    public static void subNum() {
        synchronized (lock) {
            while (num == 0) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            num--;
            try {
                Thread.sleep(0);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            lock.notifyAll();
            System.out.println(Thread.currentThread().getName() + ",当前num: " + num + ",消费: " + 1);
        }
    }

    public static void addNum() {
        synchronized (lock) {
            while (num == MAX_NUM) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            num++;
            try {
                Thread.sleep(0);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            lock.notifyAll();
            System.out.println(Thread.currentThread().getName() + ",当前num: " + num + ",生产: " + 1);
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    subNum();
                }
            });
            thread.setName("消费者" + i);
            thread.start();
        }
        for (int j = 0; j < 10; j++) {
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    addNum();
                }
            });
            t.setName("生产者" + j);
            t.start();
        }
    }
}

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Thread6 {
    private static int num = 0;
    private static int MAX_NUM = 20;
    private static Lock lock = new ReentrantLock();
    private static Condition full = lock.newCondition();
    private static Condition empty = lock.newCondition();

    public static void subNum() {
        lock.lock();
        while (num == 0) {
            try {
                empty.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        num--;
        try {
            Thread.sleep(0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        full.signalAll();
        System.out.println(Thread.currentThread().getName() + ",当前num: " + num + ",消费: " + 1);
        lock.unlock();
    }

    public static void addNum() {
        lock.lock();
        while (num == MAX_NUM) {
            try {
                full.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        num++;
        try {
            Thread.sleep(0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        empty.signalAll();
        System.out.println(Thread.currentThread().getName() + ",当前num: " + num + ",生产: " + 1);
        lock.unlock();
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    subNum();
                }
            });
            thread.setName("消费者" + i);
            thread.start();
        }
        for (int j = 0; j < 10; j++) {
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    addNum();
                }
            });
            t.setName("生产者" + j);
            t.start();
        }
    }
}

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class Thread7 {
    private static int MAX_NUM = 2;
    private static BlockingQueue<String> queue = new ArrayBlockingQueue<String>(MAX_NUM);

    public static void subNum() {
        try {
            String item = queue.take();
            System.out.println(Thread.currentThread().getName() + ",当前num: " + queue.size() + ",消费: " + 1);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void addNum() {
        try {
            queue.put("1");
            System.out.println(Thread.currentThread().getName() + ",当前num: " + queue.size() + ",生产: " + 1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    subNum();
                }
            });
            thread.setName("消费者" + i);
            thread.start();
        }
        for (int j = 0; j < 10; j++) {
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    addNum();
                }
            });
            t.setName("生产者" + j);
            t.start();
        }
    }
}

相关文章

  • 34.Python之生产者消费者模型

    Python之生产者消费者模型(非常重要) 生产者消费者模型模型指的是一种解决问题的套路。 生产者消费者模型中包含...

  • 生产者和消费者模型

    生产者和消费者模型 1. 什么是生产者和消费者模型 生产者消费者模型具体来讲,就是在一个系统中,存在生产者和消费者...

  • 生产者消费者(一)

    生产者消费者模型: 生产者------> 缓存<-------- 消费者

  • Future

    Future 模式只是生产者-消费者模型的扩展。经典“生产者-消费者”模型中消息的生产者不关心消费者何时处理完该条...

  • python入门开发学习笔记之掌握什么是生产者消费者模型

    本节重点 熟练掌握什么是生产者消费者模型熟练掌握为什么要用生产者消费者模型熟练掌握如何实现生产者消费者模型本节时长...

  • wait/notify实现生产者消费者(6)

    生产者消费者模型 生产者消费者模型是一个典型的多线程问题,涉及生产者、消费者、产品仓库。生产者生产的产品放入仓库中...

  • 生产者消费者

    生产者/消费者模式(阻塞队列) 生产者消费者模型的实现

  • 生产者消费者模型示例

    生产者消费者模型Main provider(生产者) Consumer(消费者) Data数据 log信息

  • 生产者消费者模型Java实现

    生产者消费者模型 生产者消费者模型可以描述为:①生产者持续生产,直到仓库放满产品,则停止生产进入等待状态;仓库不满...

  • 生产者与消费者模型

    生产者与消费者模型 通过使用Object的wait(),notify()方法进行生产者与消费者模型中出现的数据同步...

网友评论

      本文标题:生产者消费者模型

      本文链接:https://www.haomeiwen.com/subject/sdnldqtx.html