一个简单的程序:生产者和消费者同步问题
ProductorDemo类:
import java.util.Stack;
public class ProductorDemo implements Runnable
{

Stack<Integer> stack=new Stack<Integer>();
public void run()
{
synchronized (stack)
        {

for(int i=0;i<10;i++)
{
                                int intrand=(int)(Math.random()*10)+1;
stack.push(new Integer(intrand));
System.out.println("push:"+(int)intrand);

        }

}
}
ConsumerDemo 类:
public class ConsumerDemo implements Runnable
{
ProductorDemo productorDemo=new ProductorDemo();
public void run()
{
try
        {
        Thread.sleep(1000);
        } catch (InterruptedException e)
        {
        // TODO: handle exception
         e.printStackTrace();
        }
        synchronized(productorDemo.stack) //这样行么?如果不行,能否解释下为什么.        {
try
            {
if(productorDemo.stack.isEmpty())
{
System.out.println("There are no goods for consuming!");
Thread.sleep(1000);
}
productorDemo.stack.pop();
            } catch (InterruptedException e)
            {
            // TODO: handle exception
             e.printStackTrace();
            }
catch(Exception e)
{
e.printStackTrace();
}
//this.consume();

        }
}
}
测试类:
public class ProduceAndConsumeTest 
{ /**
 * @param args
 */
Thread productor=new Thread(new ProductorDemo());
Thread consumer=new Thread (new ConsumerDemo());
public ProduceAndConsumeTest()
{
productor.start();
consumer.start();
}
public static void main(String[] args)
{

// TODO Auto-generated method stub

new ProduceAndConsumeTest(); }}
调试结果说是java.util.EmptyStackException,哪里出错了?

解决方案 »

  1.   

    第一个类里面已经对stack做了同步限制了,这里应该不用再写这条语句了——synchronized(productorDemo.stack)
      

  2.   

    如果不加这条语句,能实现同步么?
    我是想在对临界资源stack进行进栈和出栈操作时互斥.
      

  3.   

    出现java.util.EmptyStackException这个的原因是,你的生产者和消费者没有共享一个stack
    代码我给你贴出来,改好了
      

  4.   


    import java.util.Stack;
    public class ProduceAndConsumeTest 
    {    /**
         * @param args
         */
        Thread productor=new Thread(new ProductorDemo());
        Thread consumer=new Thread (new ConsumerDemo());
        public ProduceAndConsumeTest()
        {
            productor.start();
            consumer.start();
        }
        public static void main(String[] args)
        {
            
            // TODO Auto-generated method stub
            
            new ProduceAndConsumeTest();    }}class ProductorDemo implements Runnable
    {
        
        static Stack<Integer> stack=new Stack<Integer>();
        public void run()
        {
            synchronized (stack)
            {
                
                for(int i=0;i<10;i++)
                {
                    int intrand=(int)(Math.random()*10)+1;
                    stack.push(new Integer(intrand));
                    System.out.println("push:"+(int)intrand);
                } 
            }
            
        }
    }
    class ConsumerDemo implements Runnable
    {
        //ProductorDemo productorDemo=new ProductorDemo();
        public void run()
        {
            try
            {
                Thread.sleep(1000);
            } catch (InterruptedException e)
            {
                // TODO: handle exception
                e.printStackTrace();
            }
            synchronized(ProductorDemo.stack) {
                    try
                    {
                       for(int i=0;i<10;i++){
                        if(ProductorDemo.stack.isEmpty())
                        {
                            System.out.println("There are no goods for consuming!");
                            Thread.sleep(1000);
                        }
                        Integer j=(Integer)ProductorDemo.stack.pop();
                        System.out.println("pop:"+j);
                      }
                    } catch (InterruptedException e){
                        // TODO: handle exception
                        e.printStackTrace();
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                //this.consume();         
            }
        }
    }
      

  5.   

    我在LINUX下编译,就有一个错误。ProduceAndConsumeTest.java:57: <identifier> expected
        Stack<Integer> stack=new Stack<Integer>();
             ^
    1 error