有关wait的一些问题

netapple 2002-11-18 09:37:23
public class test extends Thread
{private int i=0;
public test(){}
public void run(){
while (i<100){
try{
System.out.println(i);
wait(1000);
i++;
System.out.println(i);
}catch(Exception err){}
}
}
public static void main(String args[]) throws java.io.IOException
{test test1=new test();
test1.run();
。。。
}
}
好像吧能运行WAIT(1000)以前的,而且很快。
那么在test1.run以下的程序因为RUN没有完成而执行不完。那样的话,那部份中就算有notify();也运行不了。那我的WAIT怎么结束呀。
还有能不能说一下wait()的用法,比如wait()里的数是什么意思
...全文
29 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
tiger999 2002-11-19
  • 打赏
  • 举报
回复
wait()一般情况下用在多线程中
比如两个线程要访问一个同步对象 X
当Thread1 要访问X 时,它检查 X 是否可访问
比如访问条件是有没有别的线程正在访问 X。如果否,调用 X.wait().
假设 Thread2 此时在访问X,当Thread2 访问结束时,Thread2 调用
X.notify().这就通知Thread1 现在可以访问X了.
wait()和notify()是配对使用的.
zliang2000 2002-11-19
  • 打赏
  • 举报
回复
楼上说的很详细,非常好!!
mercury1231 2002-11-18
  • 打赏
  • 举报
回复
wait()里边的书代表了等待的时间,是以毫秒来计算的。
snowchicken 2002-11-18
  • 打赏
  • 举报
回复
如果没有其它线程调用notify or notifyAll,wait(1000)就等待 1000毫秒,才继续执行。

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧