新手学线程的问题
public class Spy {public static void main(String[] args)
{
TreeHore th=new TreeHore();
SpyMan sm=new SpyMan(th);
ImformationMan im=new ImformationMan(th);
sm.start();
im.start();
}
}
class SpyMan extends Thread
{
TreeHore th;
SpyMan(TreeHore th)
{
this.th=th;
}
public void run()
{
for(int i=0;i<10;i++)
{
th.put(i);
System.out.println("spy put"+i+"imformation");
}
}
}
class ImformationMan extends Thread
{
TreeHore th;
ImformationMan(TreeHore th)
{
this.th=th;
}
public void run()
{
th.get();
}
}
class TreeHore
{
int value;
boolean b=false;
synchronized void put(int i)
{
if(b==false)
{
this.value=i;
b=true;
notify();
}
try
{
wait();
}
catch(Exception e)
{
e.printStackTrace();
}
}
synchronized int get()
{
if(b==false)
{
try
{
wait();
}
catch(Exception e)
{
e.printStackTrace();
}
}
System.out.println("Imformation get"+value+"imformation");
b=false;
notify();
return value;
}
}
以上程序主要想实现的功能是:间谍把情报放到树洞以后,等待,然后通知情报员,接着情报员去树洞取情报,情报员取了情报以后,等待,通知间谍接着放情报,循环10次,但是我这个执行了1次好象就死锁了还是什么,就出现
Imformation get0imformation
spy put0imformation
请高手帮忙看下,谢谢