一种实现生产者-消费者问题的新方法

生产者-消费者问题,是操作系统的进程管理中很重要的问题。以前的我都是用wait(),notify()方法来实现的。今天我在翻阅JDK1.5API帮助文档的时候,偶然间发现了一个类ArrayBlockingQueue,它其中提供了以下两个方法:

void put (E o)
          Adds the specified element to the tail of this queue, waiting if necessary for space to become available.
 E take()
          Retrieves and removes the head of this queue, waiting if no elements are present on this queue.


我灵机一动,用这两个JDK中定义好的方法是不是也可以实现生产者-消费者问题呢?试了一下,嘿,还真灵。

/*
 *@author 我为J狂 建立日期 2007-3-25
 *
 
*/

package net.blogjava.lzqdiy;

import java.util.concurrent.ArrayBlockingQueue;

public class TestUtil
{
    
private ArrayBlockingQueue<String> queue ;

    
/**
     * 
@param args
     
*/

    
public TestUtil(int capacity)
    
{
        queue 
= new ArrayBlockingQueue<String>(capacity);
    }

    
class Consumer extends Thread
    
{

        @Override
        
public void run()
        
{
            
// TODO Auto-generated method stub
            while (true)
            
{
                
synchronized (queue)
                
{
                    
try
                    
{
                        System.out.println(
"take:" + queue.take());
                        Thread.sleep(
5);
                    }
 catch (InterruptedException e)
                    
{
                        
// TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }

            }

        }

    }


    
class Producer extends Thread
    
{

        
private int i;
        @Override
        
public void run()
        
{
            
// TODO Auto-generated method stub
            while (true)
            
{
                
synchronized (queue)
                
{
                    
try
                    
{
                        queue.put((
++i) + "");
                        System.out.println(
"put:" + i);
                        Thread.sleep(
1);
                    }
 catch (InterruptedException e)
                    
{
                        
// TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }

            }

        }

    }


    
public static void main(String[] args)
    
{
        TestUtil t 
= new TestUtil(10);//阻塞队列的容量为10
        TestUtil.Producer p = t.new Producer();
        p.start();
        
        TestUtil.Consumer c 
= t.new Consumer();
        c.start();
    }

}

这个算法有点小BUG,就是当队列容量过小时,有可能发生死锁。希望大家留言,来改进这个算法。

posted on 2007-03-25 14:17 我为J狂 阅读(2041) 评论(3)  编辑  收藏 所属分类: 线程

评论

# re: 一种实现生产者-消费者问题的新方法。 2007-03-25 15:05 dennis

jdk5引入的concurrent包,了解太少,这方面的学习资料也少  回复  更多评论   

# re: 一种实现生产者-消费者问题的新方法 2007-05-08 22:04 www.bushiba.com

不懂哦,只知道老师要写偶写出来  回复  更多评论   

# re: 一种实现生产者-消费者问题的新方法 2008-08-07 11:07 pei

用了BlockingQueue还要synchronized吗?  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2007年3月>
25262728123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

常用链接

留言簿(11)

随笔分类(48)

文章分类(29)

常去逛逛

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜