幾個(gè)有爭(zhēng)議的對(duì)于thread的題(轉(zhuǎn)自javaren的一篇討論)
發(fā)表時(shí)間:2023-08-19 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]formyjoy 最近做了幾套題,也在網(wǎng)上參與了大家的討論,但發(fā)現(xiàn)有幾個(gè)題大家有些爭(zhēng)議,現(xiàn)重貼如下,希望大家指正 Q1 1. public class SyncTest ****** 2. publ...
formyjoy
最近做了幾套題,也在網(wǎng)上參與了大家的討論,但發(fā)現(xiàn)有幾個(gè)題大家有些爭(zhēng)議,現(xiàn)重貼如下,希望大家指正
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.
我分析了一下,覺得有可能發(fā)生deadlock(D),另外我編譯運(yùn)行了一下,結(jié)果為CDDACB(B),
誰能具體分析一下這道題中線程執(zhí)行的過程.
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有類似之處,但好像實(shí)際上不一樣,希望各位大蝦加以討論.
-------------------------------
ericsun
關(guān)于問題3 -- 在 "一道線程題" 中出現(xiàn)過。 下面是我當(dāng)時(shí)的回答。
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
一點(diǎn)不同意見,沒有人能夠保證which thread.start write first start first,
因?yàn)镴VM控制不了何時(shí)thread投入運(yùn)行,這和具體的系統(tǒng)有關(guān)。thread.start僅僅是通知操作系統(tǒng)
我有一個(gè)thread要運(yùn)行,至于何時(shí)運(yùn)行,由操作系統(tǒng)決定。不過做題嘛,看出題人怎么考慮了,
如果你的思路和他一樣,也許就對(duì)了,不一樣,也許就錯(cuò)了。這里的6個(gè)答案,確定錯(cuò)誤的只有E,
其他的,實(shí)在是不知道。
-------------------------------------------------------------
ericsun
Q1 的分析如下:
這里顯然有個(gè)死鎖的可能。選 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();
另一個(gè) Thread 相同,就是說同時(shí)鎖住兩個(gè)變量了 s1, s2 .
那問題就簡單了。
1。如果第一個(gè)先運(yùn)行的話.
s1="A", s2="B"println s1s2 = AB
接下來,
s2="BC", s1="AD" println s2s1=BCAD
結(jié)果是 ABBCAD
2。如果第二個(gè)先運(yùn)行的話.
s2="C", s1="D" println s2s1=CD
接下來,
s1="DA", s2="CB"println s1s2 =DACB
結(jié)果是 CDDACB
所以答案是 A.B
看來有三個(gè)答案一定對(duì) A B D.
如果一定要選兩個(gè), 那就要選 D E(包含了A,B) 了。
或是 A B 了 排除 死鎖的情況。
對(duì)不對(duì)呢 cqing.順便問一下,不同的 system 會(huì)有不同的 thread model 對(duì)不對(duì)啊。
---------------------------------------------------
ericsun
Q2 的答案是 A C . 原來的 答案錯(cuò)了。
你可以這樣試一下:
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()
就可以證實(shí)你的想法了。
-------------------------------------------------
cqing
Q1我考試的時(shí)候做過,可以肯定答案是D E.
Q2我以前貼過一個(gè)在javaranch上的地址,那里有很多超級(jí)高手,可是好像也沒有定論,
我個(gè)人傾向于A C
ericsun,你說得沒錯(cuò), 不同的 system 會(huì)有不同的 thread model
--------------------------------------------------
hongwgjy
解說一下怎樣形成死鎖
--------------------------------------------------
formyjoy
比如Q1說第一個(gè)線程運(yùn)行得到s1的lock,這是第二個(gè)線程得到了s2的lock,
那么第一個(gè)線程必須等待第二個(gè)線程釋放s2的lock才能繼續(xù)運(yùn)行,而這時(shí)
第二個(gè)線程卻無法運(yùn)行下去(因?yàn)閟1的lock還被別人占著)。這樣deadlock就形成了。
這幾個(gè)題大家理解一下,考試很可能考到的。