java多線程之生產者消費者隊列

來源:互聯網
上載者:User

java多線程一般都會講消費者-生產者模型

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

----------------------------------------------------Message類

public 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;
}

}

-------------------------------------------------------------------------------------------------------------Queue類

import java.util.ArrayList;
import java.util.List;

public class Queue {
 List<Message> queue = new ArrayList<Message>();

 /** 隊列中message對象的最大值,預設為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);
  System.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.println("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類

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 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");
 
 threadWQ1.start();
 threadWQ2.start();
 
 threadRQ1.start();
  threadRQ2.start();
  threadRQ3.start();
}
}

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(100);
  } catch (Exception e) {
  }
 }

}
}

class Consumer extends Thread {
private Queue queue;

Consumer(Queue queue) {
 this.queue = queue;
}

public void run() {
 while (true) {
  queue.consume();
  try {
   sleep(100);
  } catch (Exception e) {
  }

 }
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.