Originally posted by Hades Pan:
class Happy implements Runnable {
private int x;
private int y;
public void run() {
//some code
}
public static void main(String args[]) {
Happy h=new Happy();
new Thread(h).start();
new Thread(h).start();
}
void setX(int i) {
x=i;
}
void setY(int j) {
y=j;
}
synchronized void setXY(int i) {
setX(i);
setY(i);
}
synchronized boolean check() {
return x!=y;
}
}
Select a true statement from the following.
a) check() never returns true.
b) multiple threads accessing setX(),setY() can result in check()
returning true.
c) multiple threads accessing setXY() can result in check()
returning true.
d) compilation error because there is no start() .
Which ANS above is right?
Darryl Failla
Sun Certified Java 2 Programmer
SCJP2. Please Indent your code using UBB Code
SCJP2. Please Indent your code using UBB Code
SCJP2. Please Indent your code using UBB Code
SCJP2. Please Indent your code using UBB Code
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. s2.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. }
SCJP2. Please Indent your code using UBB Code
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|