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
最近做了几套题,也在网上参与了大家的讨论,但发现有几个题大家有些争议,现重贴如下,希望大家指正
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
| 对此文章发表了评论 |
