| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1032 人关注过本帖
标题:分享:java多线程实现生产者消费者问题
只看楼主 加入收藏
随WW便
Rank: 1
来 自:山东青岛
等 级:新手上路
帖 子:46
专家分:2
注 册:2009-10-11
收藏
 问题点数:0 回复次数:3 
分享:java多线程实现生产者消费者问题
//生产者消费者问题
//生产者线程
class ProducerThread extends Thread{
    //private final Random random;
    private final Table table;
    private static int id=0;
    public ProducerThread(String name,Table table){
        super(name);
        this.table=table;
    }
    public void run(){
        try{
            while(true){
                Thread.sleep(1000);
                String cake="产品"+nextId();
                table.put(cake);
            }
        }catch(InterruptedException e){}
    }
    private static synchronized int nextId(){
        return id++;
    }
}
//消费者线程
 class ConsumerThread extends Thread{
    private final Table table;
    public ConsumerThread(String name,Table table){
        super(name);
        this.table=table;
        
    }
    public void run(){
        try{
            while(true){
                String cake=table.take();
                Thread.sleep(2000);
            }
        }catch(InterruptedException e){}
    }
}
 //缓冲池
 class Table{
     private final String[] buffer;
     private int tail;
     private int head;
     private int count;
     public Table(int count){
         this.buffer=new String[count];
         this.head=0;
         this.tail=0;
         this.count=0;
     }
     synchronized void put(String cake)throws InterruptedException{
         System.out.println(Thread.currentThread().getName()+"  放入  "+cake);
         while(count>=buffer.length){
             System.out.println(Thread.currentThread().getName()+"  缓冲池满了,等一等吧...");
             wait();
             System.out.println(Thread.currentThread().getName()+"  有空间了,开始放...");
         }
         buffer[tail]=cake;
         tail=(tail+1)%buffer.length;
         count++;
         System.out.println("缓冲池中的产品数---->"+(count+1));
         notifyAll();
     }
     synchronized String take()throws InterruptedException{
         while(count<=0){
             System.out.println(Thread.currentThread().getName()+"  缓冲池没产品了,等一等吧...");
             wait();
             System.out.println(Thread.currentThread().getName()+"  有产品了,开吃...");
         }
         String cake=buffer[head];
         head=(head+1)%buffer.length;
         count--;
         notifyAll();
         System.out.println(Thread.currentThread().getName()+"  拿走  "+cake);
         System.out.println("缓冲池中的产品数----->"+(count+1));
         return cake;
     }
 }
public class Main {
  public static void main(String args[]){
      Table table=new Table(10);
      new ProducerThread("生产者",table).start();
      new ConsumerThread("消费者",table).start();
  }
}
可以运行------>多多指正~
搜索更多相关主题的帖子: 分享 生产者 线程 java 消费者 
2009-10-16 20:00
流星雨
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:JAVA风暴
等 级:版主
威 望:43
帖 子:1854
专家分:1868
注 册:2004-5-30
收藏
得分:0 
程序的优化是没有止境的,能运行出来就行,如果你这个只是交作业的话.如果是真实的系统,你按照客户的需求来好了。

感谢你们带我找到星空下美丽神话,无论经历多少苦痛也不放弃的梦;插上希望翅膀乘风我和你们飞翔,飞过海天尽头携手把梦想实现.....
2009-10-16 22:45
pywepe
Rank: 6Rank: 6
等 级:侠之大者
威 望:4
帖 子:296
专家分:483
注 册:2009-4-5
收藏
得分:0 
回复 楼主 随WW便
这个比较简单

n个生产者

m个消费者

才更实际点

java群
62635216
欢迎加入
2009-10-17 01:41
随WW便
Rank: 1
来 自:山东青岛
等 级:新手上路
帖 子:46
专家分:2
注 册:2009-10-11
收藏
得分:0 
这个很简单啊:
new ProducerThread("生产者1",table).start();
new ProducerThread("生产者2",table).start();
new ProducerThread("生产者3",table).start();
new ProducerThread("生产者4",table).start();
new ProducerThread("生产者5",table).start();
new ConsumerThread("消费者1",table).start();
new ConsumerThread("消费者2",table).start();
new ConsumerThread("消费者3",table).start();
new ConsumerThread("消费者4",table).start();
不就行了
 

一切皆对象!!
2009-10-17 22:25
快速回复:分享:java多线程实现生产者消费者问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019526 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved