CANNOT directly cause a thread to stop

alien1 2004-08-11 04:21:55
这道题比较变态,有两个版本
I.
which two cannot directly cause a thread to stop executing?
A. exiting from a synchronized block
B. calling the wait method on an object
C. calling the notify method on an object
D. calling a read method on an InputStream object
E. calling the setPriority method on a thread object
The answer is "C,E"


II.
Which two CANNOT directly cause a thread to stop executing? (Choose Two)
A. Calling the yield method.
B. Calling the wait method on an object.
C. Calling the notify method on an object.
D. Calling the notifyAll method on an object.
E. Calling the start method on another Thread object.
Answer A,E


两道题都很搞,让我一开始最不明白的是notify()绝对不会使当前线程停止,为什么答案里都没有。难道是答案有问题。
但后来仔细一琢磨,想起了十年前早就还给高中英语老师一个语法点--部分否定。
CANNOT directly cause ,这里的意思也许并不是说“不会导致线程停止”,而是表达一个可能会让它停,但又不保证一击奏效,要视具体情况而定的意思。这样,标准答案就好理解了。

先看I.
A.如果在退出的那个syn block中调过notify(),而且那个monitor的wait set不为空,那它就挂在这儿了,否则继续走它的路---可选
B.在syn block中,放锁;不在,抛异常,反正总归是走投无路了。--不合题意,不选
C.就算要放锁,也要等syn block结束后,只要在同步块里,调用它线程根本不会停--不选
D.i/o block,线程阻塞的诱因之一,基本上都会导致线程到边儿凉快去--因为太确定,所以不选
E.setPriority的后果要看有无其它线程,其它线程的优先级和新设优先级的大小关系,还有thread scheduling是实现等,反正保证不了中止当前线程的伟大使命,但具有这种潜质,好好挖掘还有很有前途的--选


再看II.
A.yield(),move the thread from running state to ready one. 如果有其它线程ready了,那它就交权,退居二线;但如果当时没有其它线程在ready状态,那它当仁不让的继续大行其道了--可选

B、C、D前面都讲过了

E.和上面的E也差不多的道理,不考虑锁的话,起一个新线程,时间片就那么可怜巴巴的几个,就像《十面埋伏》里小妹就一个,但来了金捕头,又冒出个刘捕头一样,大家抢呗。至于谁抢得到,就看本事(Priority),和大环境(thread-scheduling)了。--它不能直接导致当前线程停止,可选



从这道题里看出,SCJP里也有这种扣字眼的题,想要高分,还要去复习一下英文语法,呵呵

不知道我的理解对不对,大家帮忙评估一下

...全文
104 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,528

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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