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();

}

}
时间: 2024-10-20 16:20:44

Which statement is true?的相关文章

which statement is true for the class java.util.ArrayList?

A.集合中的元素是有序的   对 B.集合被保证为不可变的  错 C.集合中的元素被保证为唯一的  错  有序的 所以不唯一 D.集合中的元素使用一个唯一的键来存取   错   没有键 E.集合中的元素被保证为同步的   错  不是同步的

数据结构复习2

1. 一个非空广义表的表尾(只能是子表) 解析:(1)<数据结构>对广义表的表头和表尾是这样定义的:  如果广义表LS=(a1,a2...an)非空,则 a1是LS的表头,其余元素组成的表(a2,a3,..an)是称为LS的表尾.  根据定义,非空广义表的 表头是一个元素,它 可以是原子也可以是一个子表, 而表尾则必定是子表.例如:LS=(a,b),表头为a,表尾是(b)而不是b.另外:LS=(a)的表头为a,表尾为空表(). (2)非空广义表,除表头外,其余元素构成的表称为表尾,所以非空广义

day4:vcp考试

Q61. Which two statements are true regarding Virtual SAN Fault Domains? (Choose two.)A. They enable Virtual SAN to tolerate the failure of an entire physical rack.B. Virtual SAN ensures that no two replicas are provisioned on the same domain.C. Virtu

NONUNIFORM MEMORY ACCESS

COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION In terms of commercial products, the two common approaches to providing a multiple-processor system to support applications are SMPs and clusters. For some years, another

REST Security with JWT using Java and Spring Security

Security Security is the enemy of convenience, and vice versa. This statement is true for any system, virtual or real, from the physical house entrance to web banking platforms. Engineers are constantly trying to find the right balance for the given

JDBC在springMvc等框架中使用的方式

连接池jar:c3p0 代码结构 ----------------------------------------------- 配置文件  config.properties #hibernate.dialect=sy.util.Oracle10gDialect#driverClassName=oracle.jdbc.driver.OracleDriver#validationQuery=SELECT 1 FROM DUAL#jdbc_url=jdbc:oracle:thin:@localho

备考ocp_SQL专题之DELETE

原创作品,出自 "深蓝的blog" 博客,欢迎转载,转载时请务必注明以下出处,否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/43957113 delete演示: 1.查询实验数据 2.删除表数据 3.该会话下查询数据 4.回滚数据 5.回滚后,再次查看数据 题库模拟: 167.DELETE 167. Evaluate the following DELETE statement: DELETE

log4j配置打印mybatis的sql到控制台(复制)

log4j.rootLogger=DEBUG, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=[service] %d - %c -%-4r [%t] %-5p %c %x - %m%n #log4j.app

考试题目

Q1. An administrator wants to provide users restricted access. The users should only be able to perform the followingtasks:- Create and consolidate virtual machine snapshots- Add/Remove virtual disks- Snapshot ManagementWhich default role in vCenter