用java實現的生產者與消費者多線程同步互斥模型

來源:互聯網
上載者:User

生產者與消費者模型中,要保證以下幾點:
 (1)同一時間內只能有一個生產者生產。
 (2)同一時間內只能有一個消費者消費。
 (3)生產者生產的同時消費者不能消費。
 (4)訊息佇列滿時生產者不能繼續生產,訊息佇列空時消費者不能繼續消費,此時必須等待。

代碼:

1、Message.java

package com.test;</p><p>/**<br /> * 訊息對象<br /> *<br /> */<br />public class Message {</p><p>private int id;<br />private String message;<br />public int getId() {<br />return id;<br />}<br />public void setId(int id) {<br />this.id = id;<br />}<br />public String getMessage() {<br />return message;<br />}<br />public void setMessage(String message) {<br />this.message = message;<br />}</p><p>}<br />

2、Queue.java

 package com.test;</p><p>import java.util.ArrayList;<br />import java.util.List;<br />/**<br /> * 存放訊息的緩衝隊列,共用資源<br /> *<br /> */<br />public class Queue {</p><p>private List<Message> list = new ArrayList<Message>();</p><p>private int maxCount = 5;</p><p>/**<br /> * 生產<br /> * @param message<br /> */<br />public synchronized void product(Message message){</p><p>this.notifyAll();<br />while (maxCount == getList().size()) {<br />System.out.println(Thread.currentThread().getName()+",隊列已滿,等待中。。。。");<br />try {<br />this.wait();<br />} catch (InterruptedException e) {<br />e.printStackTrace();<br />}<br />}<br />getList().add(message);<br />System.out.println(Thread.currentThread().getName()+",正在生產。。。隊列當前個數:"+getList().size());<br />}</p><p>/**<br /> * 消費<br /> */<br />public synchronized void consume(){<br />this.notifyAll();<br />while(getList().size()==0){<br />System.out.println(Thread.currentThread().getName()+",隊列為空白,等待中。。。");<br />try {<br />this.wait();<br />} catch (InterruptedException e) {<br />e.printStackTrace();<br />}<br />}<br />Message message = getList().get(0);<br />getList().remove(message);<br />System.out.println(Thread.currentThread().getName()+",正在消費。。。隊列當前個數:"+getList().size());</p><p>}</p><p>public List<Message> getList() {<br />return list;<br />}</p><p>public void setList(List<Message> list) {<br />this.list = list;<br />}</p><p>public int getMaxCount() {<br />return maxCount;<br />}</p><p>public void setMaxCount(int maxCount) {<br />this.maxCount = maxCount;<br />}<br />}<br />

3、Producer.java

package com.test;</p><p>import java.util.Random;</p><p>/**<br /> * 生產者生產<br /> *<br /> */<br />public class Producer implements Runnable {</p><p>private Queue queue = null;</p><p>public Producer(Queue queue) {<br />this.queue = queue;<br />}</p><p>public void run() {<br />while (true) {<br />Message message = new Message();<br />message.setId(new Random().nextInt());<br />message.setMessage("message," + new Random().nextInt());<br />queue.product(message);<br />try {<br />Thread.sleep(100);<br />} catch (InterruptedException e) {<br />e.printStackTrace();<br />}<br />}</p><p>}</p><p>}<br />

4、Consumer .java

package com.test;</p><p>/**<br /> * 消費者消費<br /> *<br /> */<br />public class Consumer implements Runnable {</p><p>private Queue queue;</p><p>public Consumer(Queue queue) {<br />this.queue = queue;<br />}</p><p>public void run() {</p><p>while (true) {<br />queue.consume();<br />try {<br />Thread.sleep(100);<br />} catch (InterruptedException e) {<br />e.printStackTrace();<br />}<br />}</p><p>}<br />}<br />

5、Test.java

package com.test;</p><p>public class Test {</p><p>public static void main(String[] args) {<br />Queue queue = new Queue();<br />//生產者兩個<br />Producer producer1 = new Producer(queue);<br />Producer producer2 = new Producer(queue);</p><p>// 消費者三個<br />Consumer consumer1 = new Consumer(queue);<br />Consumer consumer2 = new Consumer(queue);<br />Consumer consumer3 = new Consumer(queue);</p><p>Thread thread1 = new Thread(producer1,"producer1");<br />Thread thread2 = new Thread(producer2,"producer2");<br />Thread thread3 = new Thread(consumer1,"consumer1");<br />Thread thread4 = new Thread(consumer2,"consumer2");<br />Thread thread5 = new Thread(consumer3,"consumer3");<br />thread1.start();<br />thread2.start();<br />thread3.start();<br />thread4.start();<br />thread5.start();</p><p>}<br />}<br />  

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.