java中的生产者-消费者线程管理

java中的生产者-消费者线程管理,java,Java,我正在用java实现基本的生产者-消费者问题 但当生产者填充缓冲区时,我的代码被挂起,消费者不消费。 下面是代码 import java.util.Vector; class hi{ static int i; } class Producer implements Runnable { volatile Vector<Integer>queue; Producer(Vector<Integer> q) { q

我正在用java实现基本的生产者-消费者问题 但当生产者填充缓冲区时,我的代码被挂起,消费者不消费。 下面是代码

import java.util.Vector;

class hi{

    static int i;

}


class Producer implements Runnable
{

    volatile Vector<Integer>queue;

    Producer(Vector<Integer> q)
    {
        queue=q;    
    }

    public void run()
    {
        while(true)
        {
        try{
        produce();
        }
        catch(Exception e)
        {

        }
        }

    }

    public synchronized void produce() throws InterruptedException
    {
        while(queue.size()==5)
        {
            synchronized(queue)
            {

            queue.wait();
            }

        }

        int t=hi.i++;
        Thread.sleep(2000);
        System.out.println("adding "+t);

        queue.add(t);
        queue.notify();



    }



}

class Consumer implements Runnable
{

    volatile Vector<Integer>queue;

    Consumer(Vector<Integer> q)
    {
        queue=q;    
    }

    public void run()
    {
        while(true)
        {
            System.out.println("kaka");
        try{
        consume();
        }
        catch(Exception e)
        {

        }
        }

    }

    public synchronized void consume() throws InterruptedException
    {
        //Thread.sleep(2000);
        while(queue.size()==0)
        {
            synchronized(queue)
            {

            queue.wait();
            }
        }

        System.out.println("Removing "+queue.get(0));
        queue.remove(0);
        queue.notify();

    }


}






public class ProducerConsumer {


    public static void main(String [] args) throws InterruptedException
    {
        Vector<Integer>q=new Vector<Integer>();

        Producer p=new Producer(q);
        Thread p1=new Thread(p);
        Consumer c=new Consumer(q);
        Thread c1=new Thread(c);
        p1.start();
        Thread.sleep(2000);
        System.out.println("kailash");

        c1.start();







    }

}
import java.util.Vector;
高一班{
静态int-i;
}
类生成器实现了Runnable
{
挥发性载体;
生产者(矢量q)
{
队列=q;
}
公开募捐
{
while(true)
{
试一试{
产生();
}
捕获(例外e)
{
}
}
}
public synchronized void product()引发InterruptedException
{
while(queue.size()==5)
{
已同步(队列)
{
queue.wait();
}
}
int t=hi.i++;
《睡眠》(2000年);
系统输出打印项次(“添加”+t);
添加(t);
queue.notify();
}
}
类使用者实现Runnable
{
挥发性载体;
消费者(矢量q)
{
队列=q;
}
公开募捐
{
while(true)
{
System.out.println(“卡卡”);
试一试{
消费();
}
捕获(例外e)
{
}
}
}
public synchronized void consume()引发InterruptedException
{
//《睡眠》(2000年);
while(queue.size()==0)
{
已同步(队列)
{
queue.wait();
}
}
System.out.println(“删除”+queue.get(0));
队列。删除(0);
queue.notify();
}
}
公共类生产者消费者{
公共静态void main(字符串[]args)引发InterruptedException
{
Vectorq=新向量();
生产者p=新生产者(q);
螺纹p1=新螺纹(p);
消费者c=新消费者(q);
螺纹c1=新螺纹(c);
p1.开始();
《睡眠》(2000年);
System.out.println(“kailash”);
c1.开始();
}
}

上面我在consume method中对Thread.sleep(2000)进行了注释。如果我取消注释此代码,代码将非常正常。请帮助我理解此问题。

在调用
队列之前,您应该在
队列上进行同步。
…notify()
…感谢您现在的工作:-)您这样做是为了了解它还是为了“真正”使用它?如果是后者,您应该查看java.util.concurrent包中的类。