【发布时间】:2014-06-11 17:57:22
【问题描述】:
我有一个wait(timeout) 的测试代码。
public static void main(String[] args) throws Exception
{
Runnable r = new Runnable()
{
public void run()
{
while (true)
{
int random = (int)(Math.random() * 10);
synchronized(this)
{
try
{
wait(random);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println(random);
}
}
};
new Thread(r).start();
}
但是,这似乎无法正常工作,理想情况下它应该等待random 方法给出的特定时间并打印它。
但是每次打印几个值(随机次数)后它都会停止。
无法确定问题所在。
【问题讨论】: