答案为什么是B?

maxlong2 2007-05-22 10:19:24
void waitForSignal(){
Object obj=new Object();
synchronized(Thread.currentThread()){
obj.wait();
obj.notify();

}
}

what statement is ture?
A.this code may throw an InterruptedException.
B.this code may throw an IllegalStateException.
C.this code may throw a TimeoutException after ten minutes.
D.this code will not compile unless "obj.wait()" is replace with "((Thread)obj).wati()".
E.a call to notify() or notifyAll() from another thread may cause this method to complete normally.
...全文
244 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
superjackwang1 2012-07-29
  • 打赏
  • 举报
回复
答案是A,别争了.压根不是B.
ChDw 2007-05-23
  • 打赏
  • 举报
回复
这里应该是都不对吧
应该抛出java.lang.IllegalMonitorStateException异常

因为你在调用obj.wait();的时候并没有对obj锁定,必须
synchronized(obj)之后才可以调用obj.wait()
code_lvp 2007-05-23
  • 打赏
  • 举报
回复
UP
lionest 2007-05-23
  • 打赏
  • 举报
回复
UP
tomkai 2007-05-23
  • 打赏
  • 举报
回复
synchronized(Thread.currentThread()){
obj.wait();
obj.notify();

}
====================

obj.wait()是用于把当前线程抛入对象池等待,直到其它线程调用此对象的notify()方法将该线程唤醒....后面紧接着来了个obj.notify() 处于等待状态下的线程调用notify()方法唤醒自身?

属于非法调用
pele007 2007-05-23
  • 打赏
  • 举报
回复
IllegalStateException:
在非法或不适当的时间调用方法时产生的信号。换句话说,即 Java 环境或 Java 应用程序没有处于请求操作所要求的适当状态下。

synchronized锁住的是当前线程这个对象,而当前线程正在使用中,所以属于非法或不适当的时间表调用.

不知道解释的对否,听高手的详解.:)

62,628

社区成员

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

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