本文共 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/