class stack
{
int sip=0;
String data[]=new String[6];
public synchronized void push(String str)
{
while(sip==data.length)
{
try
{
this.wait();
}
catch(InterruptedException e){}
}
this.notify();
System.out.println("Produced:"+str); data[sip]=str;
sip++;
}
public synchronized String pop()
{
while(sip==0)
{
try
{
this.wait();
}
catch(InterruptedException e){}
}
this.notify();
sip--;
return data[sip];
}
}
class Consumer implements Runnable
{
stack stackOne;
public Consumer(stack s)
{
stackOne=s;}
public void run()
{
String strTemp=null;
for(int i=0;i<8;i++)
{
strTemp=stackOne.pop();
System.out.println("Consumed:"+strTemp);
try
{
Thread.sleep((int)(Math.random()*100));
}
catch(InterruptedException e){}
}
}
}
class Producer implements Runnable
{
stack stackOne;
public Producer(stack s)
{stackOne=s;}
public void run()
{
String strTemp=null;
for(int i=0;i<8;i++)
{
strTemp=String.valueOf(i+1);
stackOne.push(strTemp);
try
{
Thread.sleep((int)(Math.random()*100));
}
catch(InterruptedException e){}
}
}
}public class stackTest
{
public static void main(String args[])
{
stack s1=new stack();
Runnable producer=new Producer(s1);
Runnable consumer=new Consumer(s1);
Thread p=new Thread(producer);
Thread c=new Thread(consumer);
p.start();
c.start();
}
}这个是关于生产和消费的程序..
结果应该是生产
消费
生产
消费这个交替着的.可是我每次运行的结果都不一样..为什么??是程序不对..这程序是书的..
{
int sip=0;
String data[]=new String[6];
public synchronized void push(String str)
{
while(sip==data.length)
{
try
{
this.wait();
}
catch(InterruptedException e){}
}
this.notify();
System.out.println("Produced:"+str); data[sip]=str;
sip++;
}
public synchronized String pop()
{
while(sip==0)
{
try
{
this.wait();
}
catch(InterruptedException e){}
}
this.notify();
sip--;
return data[sip];
}
}
class Consumer implements Runnable
{
stack stackOne;
public Consumer(stack s)
{
stackOne=s;}
public void run()
{
String strTemp=null;
for(int i=0;i<8;i++)
{
strTemp=stackOne.pop();
System.out.println("Consumed:"+strTemp);
try
{
Thread.sleep((int)(Math.random()*100));
}
catch(InterruptedException e){}
}
}
}
class Producer implements Runnable
{
stack stackOne;
public Producer(stack s)
{stackOne=s;}
public void run()
{
String strTemp=null;
for(int i=0;i<8;i++)
{
strTemp=String.valueOf(i+1);
stackOne.push(strTemp);
try
{
Thread.sleep((int)(Math.random()*100));
}
catch(InterruptedException e){}
}
}
}public class stackTest
{
public static void main(String args[])
{
stack s1=new stack();
Runnable producer=new Producer(s1);
Runnable consumer=new Consumer(s1);
Thread p=new Thread(producer);
Thread c=new Thread(consumer);
p.start();
c.start();
}
}这个是关于生产和消费的程序..
结果应该是生产
消费
生产
消费这个交替着的.可是我每次运行的结果都不一样..为什么??是程序不对..这程序是书的..
解决方案 »
- java中定义enum枚举类型报错,望高手指教
- JAVA中问什么重写toString()?
- 为什么我下载了东西,评价过后也没有返还积分?
- 关于object的equals方法的问题.请教。。。
- 郁闷啊,环境变量!!谢谢谢谢谢谢…………!各位兄弟姐妹进来帮帮我吧!!
- 大家快来看看!!!
- 救命啊各位前辈,老大,高人都进来,解决个jsp小问题
- jb7中数据库连接问题
- PreparedStatement在Connection放到连接池后还可以使用吗?
- 哪里有 MD5 算法的Java原码?
- 用鼠标拖动JFrame框架改变大小时,不释放鼠标,系统如何实现实时更新JFrame?
- 各位高手,帮忙小弟看看这个源程序
{
int sip=0;
String data[]=new String[1];
public synchronized void push(String str)
{
while(sip==1)
{
try
{
this.wait();
}
catch(InterruptedException e){}
}
this.notify();
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
System.out.println("Produced:"+str);data[sip]=str;
sip++;
}
public synchronized String pop()
{
while(sip==0)
{
try
{
this.wait();
}
catch(InterruptedException e){}
}
this.notify();
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
sip--;
return data[sip];
}
}