博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
猿来绘Java-38-生产者消费者模型
阅读量:2095 次
发布时间:2019-04-29

本文共 2701 字,大约阅读时间需要 9 分钟。

生产者/消费者问题是线程通信的应用

1. 生产者(Productor)将接收到的消息放入缓冲区(MsgQueue),而消费者(Customer)从缓冲区取出消息进行消费,

2. 缓冲区只能存储固定数量的消息,比如20条消息,

3. 如果生产者试图放入多余20条消息,缓冲区满,会停止生产者生产消息。

4. 如果缓冲区有空位放消息,那么通知生产者继续生产;如果缓冲区没有消息,那么消费者会等待,如果缓冲区有消息了,再通知消费者消费消息。

代码示例

//ProducterConsumerTest.javapackage com.ylaihui.thread1;class MsgQueue {    private int msgcount = 0;    public final static int MSG_MAX = 200;    MsgQueue(){}    public int getMsgcount(){        return this.msgcount;    }    public void addCount(){        this.msgcount++;    }    public void reduceCount(){        this.msgcount--;    }}class Producter implements Runnable{    private MsgQueue msgqueue;    Producter(){}    Producter(MsgQueue msgqueue){        this.msgqueue = msgqueue;    }    @Override    public void run() {        while(true){            synchronized (msgqueue){                if(msgqueue.getMsgcount() < MsgQueue.MSG_MAX){                    msgqueue.addCount();                    msgqueue.notify();                    System.out.println("生产产品" + msgqueue.getMsgcount());                }                else {                    try {                        msgqueue.wait();                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        }    }}class Consumer implements Runnable{    private MsgQueue msgqueue;    Consumer(){}    Consumer(MsgQueue msgqueue){        this.msgqueue = msgqueue;    }    @Override    public void run() {        while(true){            synchronized (msgqueue){                if(msgqueue.getMsgcount() > 0) {                    System.out.println("消费产品" + msgqueue.getMsgcount());                    msgqueue.reduceCount();                    msgqueue.notify();                }else {                    try {                        msgqueue.wait();                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        }    }}public class ProducterConsumerTest {    public static void main(String[] args) {        // init the message queue        MsgQueue msgq = new MsgQueue();        Producter p = new Producter(msgq);        Consumer c = new Consumer(msgq);        Thread t1 = new Thread(p);        Thread t2 = new Thread(c);        t1.start();        t2.start();    }}

生产者消费者模型一般会涉及到三个方法

wait():一旦执行此方法,当前线程就进入阻塞状态,并释放锁(同步监视器)。

notify():一旦执行此方法,就会唤醒被wait的一个线程。如果有多个线程被wait,就唤醒优先级高的那个。

notifyAll():一旦执行此方法,就会唤醒所有被wait的线程。

 

 

sleep和wait的异同点

1.相同点:一旦执行方法,都可以使得当前的线程进入阻塞状态。

2.不同点:

1)两个方法声明的位置不同:Thread类中声明sleep() , Object类中声明wait()

2)调用的要求不同:sleep()可以在任何需要的场景下调用。 wait()必须使用在同步代码块或同步方法中

3)关于是否释放同步监视器:如果两个方法都使用在同步代码块或同步方法中,sleep()不会释放锁,wait()会释放锁。

 

 

 

 

 

 

 

转载地址:http://htuhf.baihongyu.com/

你可能感兴趣的文章
JavaScript 经典例子
查看>>
判断数据的JS代码
查看>>
js按键事件说明
查看>>
AJAX 设计制作 在公司弄的 非得要做出这个养的 真晕!
查看>>
Linux 查看文件大小
查看>>
Java并发编程:线程池的使用
查看>>
redis单机及其集群的搭建
查看>>
Java多线程学习
查看>>
检查Linux服务器性能
查看>>
Java 8新的时间日期库
查看>>
Chrome开发者工具
查看>>
【LEETCODE】102-Binary Tree Level Order Traversal
查看>>
【LEETCODE】106-Construct Binary Tree from Inorder and Postorder Traversal
查看>>
【LEETCODE】202-Happy Number
查看>>
和机器学习和计算机视觉相关的数学
查看>>
十个值得一试的开源深度学习框架
查看>>
【LEETCODE】240-Search a 2D Matrix II
查看>>
【LEETCODE】53-Maximum Subarray
查看>>
【LEETCODE】215-Kth Largest Element in an Array
查看>>
【LEETCODE】312-Burst Balloons
查看>>