一个简单的程序:生产者和消费者同步问题
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,哪里出错了?
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,哪里出错了?
我是想在对临界资源stack进行进栈和出栈操作时互斥.
代码我给你贴出来,改好了
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();
}
}
}
Stack<Integer> stack=new Stack<Integer>();
^
1 error