java多线程消费者Word下载.docx
- 文档编号:4060696
- 上传时间:2023-05-02
- 格式:DOCX
- 页数:11
- 大小:16.33KB
java多线程消费者Word下载.docx
《java多线程消费者Word下载.docx》由会员分享,可在线阅读,更多相关《java多线程消费者Word下载.docx(11页珍藏版)》请在冰点文库上搜索。
returncontent;
}
publicvoidsetContent(Stringcontent){
this.content=content;
publicintgetId(){
returnid;
publicvoidsetId(intid){
Message.id=id;
----------------------------------------------------Queue类
importjava.util.ArrayList;
importjava.util.List;
publicclassQueue{
List<
Message>
queue=newArrayList<
();
/**队列中message对象的最大值,默认为5*/
intmaxMessageNum=5;
publicsynchronizedvoidproduce(Messagemessage){
this.notifyAll();
while(queue.size()==maxMessageNum){
System.out.println(Thread.currentThread().getName()
+"
队列满!
等待中。
。
"
);
try{
this.wait();
}catch(InterruptedExceptione){
e.printStackTrace();
}
queue.add(message);
System.out.println(Thread.currentThread().getName()+"
正在生产"
+message.getContent()+"
当前个数:
+getCount());
publicsynchronizedvoidconsume(){
while(queue.size()==0){
队列空!
System.out.println("
begin!
wait();
end!
Messagemessage=queue.get(0);
queue.remove(0);
正在消费"
"
publicsynchronizedintgetCount(){
returnqueue.size();
----------------------------------------------------Test类
publicclassTest{
publicstaticvoidmain(String[]args){
QueueQ=newQueue();
ProducerwQ1=newProducer(Q);
ProducerwQ2=newProducer(Q);
ConsumerrQ1=newConsumer(Q);
ConsumerrQ2=newConsumer(Q);
ConsumerrQ3=newConsumer(Q);
ThreadthreadWQ1=newThread(wQ1,"
thread-wQ1"
ThreadthreadWQ2=newThread(wQ2,"
thread-wQ2"
ThreadthreadRQ1=newThread(rQ1,"
thread-rQ1"
ThreadthreadRQ2=newThread(rQ2,"
thread-rQ2"
ThreadthreadRQ3=newThread(rQ3,"
thread-rQ3"
threadWQ1.start();
threadWQ2.start();
threadRQ1.start();
threadRQ2.start();
threadRQ3.start();
classProducerextendsThread{
privateQueuequeue;
Producer(Queuequeue){
this.queue=queue;
publicvoidrun(){
while(true){
Messagemessage=newMessage();
message.setId(++Message.id);
message.setContent("
food"
+Message.id);
queue.produce(message);
sleep(100);
}catch(Exceptione){
classConsumerextendsThread{
Consumer(Queuequeue){
queue.consume();
为什么在consume方法和produce方法开始的时候要调用this.notifyAll();
这个应该是生产者在生产完产品后调用通知其他线程,同样消费者在消费完产品后也要调用this.notifyAll();
方法来通知其他线程,为什么一上来调用它呢?
我认为在consume和produce的结尾分别获得producer和consumer的锁,然后notify它们。
在方法一开始唤醒不知道有什么意义。
---------------------------------------------
除了4和5,其他都不是必要条件.实际上,更多的生产者消费者应用都不遵循1,2,3.
问下各位,“
publicsynchronizedvoidproduce(Messagemessage){”
该方法锁定的资源是“message”还是“queue”?
下面的是我写的:
publicclassProductQueue{
privateList<
Product>
products;
privateintmaxSize;
publicProductQueue(intmaxSize){
this.maxSize=maxSize;
products=newArrayList<
(maxSize);
publicsynchronizedvoidaddProdcut(Productproduct){
while(isFull()){
try{
wait();
}catch(InterruptedExceptione){
e.printStackTrace();
products.add(product);
System.out.println("
Produce:
+product.getId()+"
+product.getMadeDate());
notifyAll();
publicsynchronizedvoidremoveProduct(){
while(isEmpty()){
Productproduct=products.get(products.size()-1);
products.remove(product);
Consume:
publicsynchronizedbooleanisFull(){
booleanisFull=
products.size()==maxSize;
if(isFull){
Thequeueisfull."
returnisFull;
publicsynchronizedbooleanisEmpty(){
booleanisEmpty=products.size()<
=0;
if(isEmpty){
Thequeueisempty."
returnisEmpty;
publicclassConsumerimplementsRunnable{
privateProductQueuequeue;
publicConsumer(ProductQueuequeue){
this.queue=queue;
@Override
while(true){
queue.removeProduct();
Thread.sleep(100l);
importjava.util.Date;
publicclassProducerimplementsRunnable{
privatestaticintid;
publicProducer(ProductQueuequeue){
Productproduct=newProduct(id++,newDate());
queue.addProdcut(product);
Thread.sleep(100);
publicclassProduct{
privateintid;
privateDatemadeDate;
publicProduct(intid,DatemadeDate){
this.id=id;
this.madeDate=madeDate;
returnid;
publicDategetMadeDate(){
returnmadeDate;
publicvoidsetMadeDate(DatemadeDate){
publicclassThreadMain{
ProductQueuequeue=newProductQueue(50);
for(inti=0;
i<
10;
i++){
Producerproducer=newProducer(queue);
newThread(producer).start();
Consumerconsumer=newConsumer(queue);
newThread(consumer).start();
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 多线程 消费者