您现在的位置: 无忧电子商务网 >> 信息学院 >> 程序开发 >> js >> 正文

几个有争议的关于thread的题(转自javaren的一篇讨论)

作者:作者:未…    信息学院来源:网络收集    点击数:    更新时间:2006-8-27 我要参与讨论

  formyjoy
最近做了几套题,也在网上参与了大家的讨论,但发现有几个题大家有些争议,现重贴如下,希望大家指正
Q1
1. public class SyncTest{         ******
2. public static void main(String[] args) {
3. final StringBuffer s1= new StringBuffer();
4. final StringBuffer s2= new StringBuffer();
5. new Thread () {
6. public void run() {
7. synchronized(s1) {
8. s1.append("A");
9. synchronized(s2) {
10. s2.append("B");
11. System.out.print(s1);
12. System.out.print(s2);
13. }
14. }
15. }
16. }.start();
17. new Thread() {
18. public void run() {
19. synchronized(s2) {
20. s2.append("C");
21. synchronized(s1) {
22. s1.append("D");
23. System.out.print(s2);
24. System.out.print(s1);
25. }
26. }
27. }
28. }.start();
29. }
30. }
Which two statements are true? (Choose Two)
A. The program prints "ABBCAD"
B. The program prints "CDDACB"
C. The program prints "ADCBADBC"
D. The output is a non-deterministic point because of a possible deadlock condition
E. The output is dependent on the threading model of the system the program is running on.
我分析了一下,觉得有可能发生deadlock(D),另外我编译运行了一下,结果为CDDACB(B),
谁能具体分析一下这道题中线程执行的过程.

Q2
class s implements Runnable{
int x=0,y=0;
synchronized void addX(){x++; }
synchronized void addY(){y++; }
void addXY(){x++;y++;}
boolean check() { return (x>y)? true:false;)
public void run() {
// ?
System.out.println(check()); }
public static void main(String args[])
{ s run=new s();
Thread t1=new Thread(run);
Thread t2=new Thread(run);
t1.start();
t2.start();
}
}
If this methods are called in which order the check will return true?
Select all that apply
A.call addX() and addY() simultaneously for number of times in run()
B.call addY() and addX() simultaneously for number of times in run()
C.all addXY() for number of times in run()
Ans:B,C
C没有问题,B?A,为何B也正确,而且是先访问addY();
I think in all cases, check() can return true.
because addX() and addY() are synchronized but run is not synchronized,
hence it is very much possible that after calling addX() another thread
starts execution and increment the x again and then check() will return
true(This explanation is true for first two cases).
For third case as it(addXY()) is not synchronized so any thread can corrupt
the data, and check() can return true.

Q3
class Happy extends Thread {
final StringBuffer sb1 = new StringBuffer();
final StringBuffer sb2 = new StringBuffer();
public static void main(String args[]) {
final Happy h=new Happy();
System.out.println(h);
new Thread() {
public void run(){
System.out.println(this);
synchronized(this) {
h.sb1.append("A");
h.sb2.append("B");
Syst

[1] [2] [3] 下一页

在google里搜索更多几个有争议的关于thread的题(转自javaren的一篇讨论)

Google
Web www.51ec.org
【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
我来说两句 对此文章发表了评论
  昵 称: *必填    ·注册用户·
  评 分: 1分 2分 3分 4分 5分     严禁发表危害国家安全、政治、黄色淫秽等内容的评论,用户需对自己在使用本网站服务过程中的行为承担法律责任。本站管理员有权保留或删除评论内容,评论内容只代表机友个人观点,与本网站立场无关。  
评 论
内 容

 
评论列表 (最新 评论仅限网友观点!)

推荐文章
  • 此栏目下没有推荐信息学院
  • 供求信息




    | 设为首页 | 加入收藏 | 关于我们 | 广告服务 | 联系方式 | 友情链接 | 版权申明