Java多线程: 生产者消费者问题(源码)

有人反映看不到源码,发现是CSDN的代码控件有问题。希望CSDN好好改进一下。

暂时先以文本方式发布出来,格式会变样。

 

import java.util.LinkedList;

public class ProducerConsumer
{

 /**
  * @param args
  */
 public static void main(String[] args)
 {
  Queue queue = new Queue();
  
  Producer producer1 = new Producer(queue, "producer1");
  Producer producer2 = new Producer(queue, "producer2");
  
  Consumer consumer1 = new Consumer(queue, "consumer1");
  Consumer consumer2 = new Consumer(queue, "consumer2");
  Consumer consumer3 = new Consumer(queue, "consumer3");
  
  producer1.start();
  producer2.start();
  consumer1.start();
  consumer2.start();
  consumer3.start();
 }
}

class Message
{
 public static int sno = 0;
 
 public int id = 0;
 
 public String content = null;
 
 public Message(String content)
 {
  synchronized(Message.class)
  {
   this.id = ++sno;
  }  
  this.content = content;
 }
 public String toString()
 {
  return "id = " + id + ", content = " + content;
 }
}


class Queue
{
 private LinkedList queue = null;
 private int capacity = 0;
 
 public Queue(int capacity)
 {
  this.capacity = capacity;
  queue = new LinkedList();
 }
 public Queue()
 {
  this(10);
 }
 
 public synchronized void put(Message product)
 {
  //wait until the consumer has consume a product
  while(queue.size() >= capacity)
  {
   try
   {
    wait();
   }
   catch (InterruptedException e)
   {
    e.printStackTrace();
   }
  }
  
  queue.addLast(product);
  // notify the Consumer that a message has been put in the queue
  notifyAll();
 }
 
 public synchronized Message get()
 {
  while(queue.isEmpty())
  {
   try
   {
    wait();
   }
   catch (InterruptedException e)
   {
    e.printStackTrace();
   }
  }
  
  Message product = (Message)queue.removeFirst();
  // notify the Producer that a message has been removed
  notifyAll();
  
  return product;
 }
 
 public synchronized boolean isEmpty()
 {
  return queue.isEmpty();
 }
}
class Producer extends Thread
{
 private Queue queue = null;
 public Producer(Queue queue, String name)
 {
  this.queue = queue;
  setName(name);
 }
 public void run()
 {
  for(int i = 0; i < 10; i++)
  {
   try
   {
    sleep(100);
   }
   catch (Exception e)
   {
    e.printStackTrace();
   }
   
   queue.put(produce());
  }
 }
 public Message produce()
 {
  Message product = new Message("Made by " + getName());
  System.out.println(this.getName() + " -- Produce: " + product);
  
  return product;
 }
}

class Consumer extends Thread
{
 private Queue queue = null;
 
 public Consumer(Queue queue, String name)
 {
  this.queue = queue;
  this.setName(name);
 }
 
 public void run()
 { 
  while(true)
  {
   try
   {
    sleep(100);
   }
   catch (Exception e)
   {
    e.printStackTrace();
   }
   
   //when get a terminated message, stop the thread.
   if(!consume(queue.get()))
   {
    break;
   }
  } 
 }
 
 public boolean consume(Message product)
 {
  System.out.println(this.getName() + " -- Consume: " + product);
  
  //terminated message.
  if(product == null || product.id == 20)
  {
   queue.put(null);
   return false;
  }
  
  return true;
 }
}

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值