ImageVerifierCode 换一换
格式:DOCX , 页数:11 ,大小:16.33KB ,
资源ID:11982879      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/11982879.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(java多线程消费者.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

java多线程消费者.docx

1、java多线程消费者java多线程 消费者-生产者java多线程一般都会讲消费者-生产者模型生产者与消费者模型中,要保证以下几点:1 同一时间内只能有一个生产者生产2 同一时间内只能有一个消费者消费3 生产者生产的同时消费者不能消费4 消息队列满时生产者不能继续生产5 消息队列空时消费者不能继续消费参考了下网上一个代码实例 发现作者写得有问题 修改了一些代码 现在ok了-Message类package com.example.test;public class Message public static int id;public String content;public String ge

2、tContent() return content;public void setContent(String content) this.content = content;public int getId() return id;public void setId(int id) Message.id = id;-Queue类package com.example.test;import java.util.ArrayList;import java.util.List;public class Queue List queue = new ArrayList();/* 队列中messag

3、e对象的最大值,默认为5 */int maxMessageNum = 5;public synchronized void produce(Message message) this.notifyAll(); while (queue.size() = maxMessageNum) System.out.println(Thread.currentThread().getName() + 队列满!等待中。); try this.wait(); catch (InterruptedException e) e.printStackTrace(); queue.add(message); Syst

4、em.out.println(Thread.currentThread().getName() + 正在生产 + message.getContent() + 。 ,当前个数: + getCount();public synchronized void consume() this.notifyAll(); while (queue.size() = 0) System.out.println(Thread.currentThread().getName() + 队列空!等待中。); try System.out.println(begin!); wait(); System.out.prin

5、tln(end!); catch (InterruptedException e) e.printStackTrace(); Message message = queue.get(0); queue.remove(0); System.out.println(Thread.currentThread().getName() + 正在消费 + message.getContent() + 。 ,当前个数: + getCount();public synchronized int getCount() return queue.size();-Test类package com.example.t

6、est;public class Test public static void main(String args) Queue Q = new Queue(); Producer wQ1 = new Producer(Q); Producer wQ2 = new Producer(Q); Consumer rQ1 = new Consumer(Q); Consumer rQ2 = new Consumer(Q); Consumer rQ3 = new Consumer(Q); Thread threadWQ1 = new Thread(wQ1, thread-wQ1); Thread thr

7、eadWQ2 = 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); threadWQ1.start(); threadWQ2.start(); threadRQ1.start(); threadRQ2.start(); threadRQ3.start();class Producer extends T

8、hread 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(100); catch (Exception e) class Consumer extends Thread private Queue

9、queue;Consumer(Queue queue) this.queue = queue;public void run() while (true) queue.consume(); try sleep(100); catch (Exception e) 为什么在consume方法和produce方法开始的时候要调用 this.notifyAll(); 这个应该是生产者在生产完产品后调用通知其他线程,同样消费者在消费完产品后也要调用 this.notifyAll();方法来通知其他线程,为什么一上来调用它呢?我认为在consume和produce的结尾分别获得producer和consu

10、mer的锁,然后notify它们。在方法一开始唤醒不知道有什么意义。java多线程一般都会讲消费者-生产者模型生产者与消费者模型中,要保证以下几点:1 同一时间内只能有一个生产者生产2 同一时间内只能有一个消费者消费3 生产者生产的同时消费者不能消费4 消息队列满时生产者不能继续生产5 消息队列空时消费者不能继续消费-除了4和5,其他都不是必要条件.实际上,更多的生产者消费者应用都不遵循1,2,3.问下各位,“ public synchronized void produce(Message message) ” 该方法锁定的资源是“message”还是“queue”?下面的是我写的:impo

11、rt java.util.ArrayList;import java.util.List;public class ProductQueue private List products;private int maxSize;public ProductQueue(int maxSize)this.maxSize = maxSize;products = new ArrayList(maxSize);public synchronized void addProdcut(Product product)while(isFull()try wait(); catch (InterruptedEx

12、ception e) e.printStackTrace();products.add(product);System.out.println(Produce: + product.getId() + + product.getMadeDate();notifyAll();public synchronized void removeProduct()while(isEmpty()try wait(); catch (InterruptedException e) e.printStackTrace();Product product = products.get(products.size(

13、) - 1);products.remove(product);System.out.println(Consume: + product.getId() + + product.getMadeDate();notifyAll();public synchronized boolean isFull()boolean isFull = products.size() = maxSize;if(isFull)System.out.println(The queue is full.);return isFull;public synchronized boolean isEmpty()boole

14、an isEmpty = products.size() = 0;if(isEmpty)System.out.println(The queue is empty.);return isEmpty;public class Consumer implements Runnableprivate ProductQueue queue;public Consumer(ProductQueue queue)this.queue = queue;Overridepublic void run() while(true)queue.removeProduct();try Thread.sleep(100

15、l); catch (InterruptedException e) e.printStackTrace();import java.util.Date;public class Producer implements Runnableprivate static int id;private ProductQueue queue;public Producer(ProductQueue queue)this.queue = queue;Overridepublic void run() while(true)Product product = new Product(id+,new Date

16、();queue.addProdcut(product);try Thread.sleep(100); catch (InterruptedException e) e.printStackTrace();import java.util.Date;public class Product private int id;private Date madeDate;public Product(int id, Date madeDate)this.id = id;this.madeDate = madeDate;public int getId() return id;public void s

17、etId(int id) this.id = id;public Date getMadeDate() return madeDate;public void setMadeDate(Date madeDate) this.madeDate = madeDate;public class ThreadMain public static void main(String args) ProductQueue queue = new ProductQueue(50);for(int i = 0; i 10; i+)Producer producer = new Producer(queue);new Thread(producer).start();for(int i = 0; i 10; i+)Consumer consumer = new Consumer(queue);new Thread(consumer).start();

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1