Thread生产者消费者例子
2010-07-14 16:31
239 查看
转自:------------------------http://z-jianwen.javaeye.com/blog/616761
java多线程一般都会讲消费者-生产者模型
生产者与消费者模型中,要保证以下几点:
1 同一时间内只能有一个生产者生产
2 同一时间内只能有一个消费者消费
3 生产者生产的同时消费者不能消费
4 消息队列满时生产者不能继续生产
5 消息队列空时消费者不能继续消费
java多线程一般都会讲消费者-生产者模型
生产者与消费者模型中,要保证以下几点:
1 同一时间内只能有一个生产者生产
2 同一时间内只能有一个消费者消费
3 生产者生产的同时消费者不能消费
4 消息队列满时生产者不能继续生产
5 消息队列空时消费者不能继续消费
# public class Producer_Consumer_Test { # public static void main(String[] args) { # # Queue Q = new Queue(); # # Producer wQ1 = new Producer(Q); # Producer wQ2 = new Producer(Q); # Producer wQ3 = new Producer(Q); # // Producer wQ4 = new Producer(Q); # // Producer wQ5 = new Producer(Q); # # Consumer rQ1 = new Consumer(Q); # Consumer rQ2 = new Consumer(Q); # Consumer rQ3 = new Consumer(Q); # Consumer rQ4 = new Consumer(Q); # Consumer rQ5 = new Consumer(Q); # # // Thread threadWQ1 = new Thread(wQ1, "thread-wQ1"); # // Thread threadWQ2 = new Thread(wQ2, "thread-wQ2"); # // # // Thread threadRQ1 = new Thread(rQ1, "thread-rQ1"); # // Thread threadRQ2 = new Thread(rQ2, "thread-rQ2"); # // Thread threadRQ3 = new Thread(rQ3, "thread-rQ3"); # # wQ1.start(); # wQ2.start(); # wQ3.start(); # // wQ4.start(); # // wQ5.start(); # # rQ1.start(); # rQ2.start(); # rQ3.start(); # rQ4.start(); # rQ5.start(); # } # } # /** # * 队列 # * @author DELL # * # */ # class Queue { # List<Message> queue = new ArrayList<Message>(); # # /** 队列中message对象的最大值,默认为5 */ # int maxMessageNum = 5; # //生产产品 # public synchronized void produce(Message message) { # while (queue.size() == maxMessageNum) { # System.out.println(Thread.currentThread().getName() # + " 队列满!等待中。。。"); # try { # this.wait(); # } catch (InterruptedException e) { # e.printStackTrace(); # } # } # queue.add(message); # System.out.println(Thread.currentThread().getName() + "正在生产" # + message.getContent() + "。。。 ,当前个数:" + getCount()); # this.notifyAll(); # # } # //消费产品 # public synchronized void consume() { # while (queue.size() == 0) { # System.out.println(Thread.currentThread().getName() # + " 队列空!等待中。。。"); # try { # wait(); # } catch (InterruptedException e) { # e.printStackTrace(); # } # } # Message message = queue.get(0); # queue.remove(0); # System.out.println(Thread.currentThread().getName() + "正在消费" # + message.getContent() + "。。。 ,当前个数: " + getCount()); # this.notifyAll(); # # } # # public synchronized int getCount() { # return queue.size(); # } # } # /** # * 生产者 # * @author DELL # * # */ # class Producer extends Thread { # # private Queue queue; # # Producer(Queue queue) { # this.queue = queue; # } # # public void run() { # # while (true) { # Message message = new Message(); # message.setId(++Message.id); # message.setContent("food" + Message.id); # queue.produce(message); # try { # sleep(1000); # } catch (Exception e) { # } # } # # } # } # # /** # * 消费者 # * @author qinglong # * # */ # class Consumer extends Thread { # private Queue queue; # # Consumer(Queue queue) { # this.queue = queue; # } # # public void run() { # while (true) { # queue.consume(); # try { # sleep(1000); # } catch (Exception e) { # } # # } # } # } # /** # * 消息类 # * @author qinglong # * # */ # class Message { # public static int id; # public String content; # # public String getContent() { # return content; # } # # public void setContent(String content) { # this.content = content; # } # # public int getId() { # return id; # } # # public void setId(int id) { # Message.id = id; # }
相关文章推荐
- 在python中实现生产者和消费者的例子(四):使用thread模块和全局变量
- Java多线程Thread-并发协作(生产者消费者设计模式)
- 多线程生产者和消费者例子
- 生产者消费者例子-采用wait()和notify()
- Java多线程生产者和消费者的例子
- 笔试题11:编写一个生产者与消费者的多线程例子(源码)
- 多线程应用例子2—消费者,生产者,多个缓冲区
- 在python中实现生产者和消费者的例子(二):使用multiprocessing和queue
- 在python中实现生产者和消费者的例子(三):使用fork和pipe
- java多线程同步以及线程间通信详解&消费者生产者模式&死锁&Thread.join()(多线程编程之二)
- 生产者和消费者的简单例子
- 一个简单的生产者与消费者的多线程例子(二):不让消费者或生产者无限期地等待
- C# Thread 多线程 Monitor 锁 Producer And Consumer 生产者和消费者 经典模型
- boost学习之-生产者消费者问题例子
- 多个生产者-一个消费者-进程间同步的例子 - OutputDebugString的实现
- Java生产者消费者例子
- 多线程 :消费者生产者 --线程通讯例子
- 生产者-消费者的经典例子
- 生产者,消费者的几个例子
- java多线程同步问题(生产者和消费者)典型例子