Which statement is true?

Posted 美味的花

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Which statement is true?相关的知识,希望对你有一定的参考价值。

void waitForSignal()
{
    Object obj = new Object();
    synchronized(Thread.currentThread())
    {
        obj.wait();
        obj.notify();
    }
}

 

This code may throw an InterruptedException
This code may throw an IllegalStateException
This code may throw a TimeOutException after ten minutes
This code will not compile unless”obj.wait()”is replaced with”(Thread)obj).wait()”
Reversing the order of obj.wait()and obj.notify()may cause this method to complete normally

解析:

 

这题有两个错误的地方,第一个错误是 wait() 方法要以 try/catch 包覆,或是掷出 InterruptedException 才行   
因此答案就是因为缺少例外捕捉的   InterruptedException

第二个错误的地方是, synchronized 的目标与 wait() 方法的物件不相同,会有 IllegalMonitorStateException ,不过 InterruptedException 会先出现,所以这不是答案

最后正确的程式码应该是这样:   
  

   void waitForSignal() {

Object obj = new Object();

         synchronized (obj) {

             try {

obj.wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

obj.notify();

}

}

 

以上是关于Which statement is true?的主要内容,如果未能解决你的问题,请参考以下文章

Can’t update table ‘xxx’ in stored function/trigger because it is already used by statement which in

Part of defining a topology is specifying for each bolt which streams it should receive as input(示例代

devmapper: Thin Pool has 154464 free data blocks which is less than minimum required 163840 free dat

Groovy集合遍历 ( 使用集合的 find 方法查找集合元素 | 闭包中使用 == 作为查找匹配条件 | 闭包中使用 is 作为查找匹配条件 | 闭包使用 true 作为条件 | 代码示例 )(代

Symfony Component HttpKernel Exception AccessDeniedHttpException This action is unauthorized(示例代

can't find which disk is full