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