六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

幾個有爭議的對于thread的題(轉自javaren的一篇討論)

[摘要]formyjoy 最近做了幾套題,也在網上參與了大家的討論,但發現有幾個題大家有些爭議,現重貼如下,希望大家指正 Q1 1. public class SyncTest ****** 2. publ...
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");
System.out.println(h.sb1);
System.out.println(h.sb2);
}
}
}.start();
new Thread() {
public void run() {
System.out.println(this);
synchronized(this) {
h.sb1.append("D");
h.sb2.append("C");
System.out.println(h.sb2);
System.out.println(h.sb1);
}
}
}.start(); 
}
}
What may be the output of this code ?(Choose two)
a) ABBCAD
b) ABCBCAD
c) CDADACB
d) CDDACB
e) Output non-deterministic because of the chance of deadlock?
f) Output determined due to the underlying platFORM.
這道題與Q1有類似之處,但好像實際上不一樣,希望各位大蝦加以討論.
-------------------------------
ericsun
關于問題3 -- 在 "一道線程題" 中出現過。 下面是我當時的回答。

to cqing & stonely  
I agree with you. From logical judgement, a, b, c, d are all can be happened.
But in compile time or run time, perhaps which thread.start write first should
be start first at first one sentence. So if really need to choose two question,
I select a and b.
If the question change like ::

public void run(){
System.out.println(this);

// i add some waste thing.
for ( int i=0;i<300;i++) { int j=0; j=i; }

synchronized(this) {

h.sb1.append("A");

h.sb2.append("B");

System.out.println(h.sb1);

System.out.println(h.sb2);

}

The question's answer perhaps will be a,b,c,d.
-------------------------------------------------------------
cqing
一點不同意見,沒有人能夠保證which thread.start write first start first,
因為JVM控制不了何時thread投入運行,這和具體的系統有關。thread.start僅僅是通知操作系統
我有一個thread要運行,至于何時運行,由操作系統決定。不過做題嘛,看出題人怎么考慮了,
如果你的思路和他一樣,也許就對了,不一樣,也許就錯了。這里的6個答案,確定錯誤的只有E,
其他的,實在是不知道。
-------------------------------------------------------------
ericsun
Q1 的分析如下:
這里顯然有個死鎖的可能。選 D .
然后排除死鎖的情況。
原題等同與

new Thread () {
6. public void run() {
7. synchronized(s1,s2) { // 形式話語言。
8. s1.append("A");
10. s2.append("B");
11. System.out.print(s1);
12. System.out.print(s2);
13. }
15. }
16. }.start();

另一個 Thread 相同,就是說同時鎖住兩個變量了 s1, s2 .

那問題就簡單了。

1。如果第一個先運行的話.
s1="A", s2="B"println s1s2 = AB
接下來,
s2="BC", s1="AD" println s2s1=BCAD

結果是 ABBCAD


2。如果第二個先運行的話.
s2="C", s1="D" println s2s1=CD
接下來,
s1="DA", s2="CB"println s1s2 =DACB

結果是 CDDACB

所以答案是 A.B

看來有三個答案一定對 A B D.
如果一定要選兩個, 那就要選 D E(包含了A,B) 了。
或是 A B 了 排除 死鎖的情況。

對不對呢 cqing.順便問一下,不同的 system 會有不同的 thread model 對不對啊。
---------------------------------------------------
ericsun
Q2 的答案是 A C . 原來的 答案錯了。
你可以這樣試一下:
synchronized void addX(){
x++;
try{Thread.sleep(100); }catch(Exception e){}
}
synchronized void addY(){
y++;
try{Thread.sleep(100); }catch(Exception e){}
}

然后 in run() addX() , addY() or addY() addX()

就可以證實你的想法了。
-------------------------------------------------
cqing  

Q1我考試的時候做過,可以肯定答案是D E.
Q2我以前貼過一個在javaranch上的地址,那里有很多超級高手,可是好像也沒有定論,
我個人傾向于A C
ericsun,你說得沒錯, 不同的 system 會有不同的 thread model
--------------------------------------------------
hongwgjy  
解說一下怎樣形成死鎖
--------------------------------------------------
formyjoy
比如Q1說第一個線程運行得到s1的lock,這是第二個線程得到了s2的lock,
那么第一個線程必須等待第二個線程釋放s2的lock才能繼續運行,而這時
第二個線程卻無法運行下去(因為s1的lock還被別人占著)。這樣deadlock就形成了。
這幾個題大家理解一下,考試很可能考到的。 


主站蜘蛛池模板: 亚洲成av人片在线观看无码 | 色视频线观看在线播放 | 午夜国产高清精品一区免费 | 影院理论 | 欧美一区二区三区男人的天堂 | 色黄网站青青草原免费 | 日韩高清免费在线观看 | 亚洲天堂ww | 色.www| 日韩三级视频 | 欧美一级美片在线观看免费 | 日本丰满bbb | 青青草偷拍视频 | 日本一区视频在线 | 天天做夜夜做久久做狠狠 | 亚洲精品乱码久久久久久麻豆 | 亚洲专区在线播放 | 人妖欧美一区二区三区四区 | 四虎网站在线播放 | 一区二区视频在线播放 | 日韩高清在线高清免费 | 日本在线视频二区 | 四虎精品8848ys一区二区 | 羞羞答答免费人成黄页在线观看国产 | 日韩中文字幕在线不卡 | 日产精品一二三四区国产 | 亚洲欧美日韩不卡一区二区三区 | 视频二区在线观看 | 日韩亚洲欧美日本精品va | 污在线观看视频 | 色综色天天综合网 | 人碰人操| 欧洲97色综合成人网 | 欧美一级淫片免费播放口 | 亚洲欧洲免费 | 欧美限制级在线 | 视频在线a | 日韩成人在线影院 | 在线你懂得 | 欧美视频日韩专区午夜 | 日韩伦理在线视频 |