• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Quession obout synchronized

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?


I believe its 'a'.
If you just run this code nuthing happens..
run() method must include
setXY(1);
System.out.println(check());
HIH
Ragu
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While it is correct that running this code produces nothing, I believe the intent is to assume that calls to setX(int), setY(int), and setXY(int) are included in "some code" of run(). If this is true, then the answer would be 'b'.
Since setX() and setY() are not synchronized, they can be changed independently of each other with explicit calls to those methods. The problem is compounded by the fact that setX() and setY() have default access (no absolute control from within the class). With that being the case, x and y can have different values, and check() can return true.
 
Hades Pan
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And to see it which one is ANS?
1. public class SyncTest (
2. private int x;
3. private int y;
4. private synchronized void setX (int i) (x=1; )
5. private synchronized void setY (int i) (y=1; )
6. public void setXY(int i)(set X(i); setY(i) ; )
7. public synchronized Boolean check() (return x !=y; )
8. )
Under which conditions will check () return true when called from a different class?
A. Check() can never return true
B. Check() can return true when setXY is called by multiple threads
C. Check() can return true when multiple threads call setX and setY separately.
D. Check() can only return true if SyncTest is changed to allow x and y to be set separately.
[Val edited the code]

[This message has been edited by Valentin Crettaz (edited December 05, 2001).]
 
Hades Pan
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And please to see this one
##
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. }
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.
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that the answer is D.
The output is a non-deterministic point because of a possible deadlock condition
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Happy implements Runnable {
private int x;
private int y;
public void run() {
setXY(1);
System.out.println(check());
//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;
}
}
check() always returns false...
 
Hades Pan
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think ANS of my quession 1 and quession 2 is A. Check() can never return true.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the first code
There are three methods to modify the value of x and y. Because they are not synchronized they could be executed concurrentlty. Thus only one thing could be stated: the order of updating the values is undefined.
"a) check() never returns true."
False. You can write calls that produce true. Also you can do it the other way round. So this is false.
b) multiple threads accessing setX(),setY() can result in check()
returning true.
Yes it's true because there is no coordination between the calls to these methods and check. The last could ocurr between the two previous.
c) multiple threads accessing setXY() can result in check()
returning true.
True. Because setX and setY are not synchronized and they are not private, they could be called between the two calls within setXY, efectively setting x or y to a diferent value than the parameter to that execution of setXY.
If you want check to return always false. Synchronize all the methods on the same lock for a given object. Or call only the synchronized ones, as I have seen in an example above.

 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the second code
"A. Check() can never return true"
false, please continue reading
B. Check() can return true when setXY is called by multiple threads
Yes it's true. Because setXY is not synchronized, just imagine a thread calling setX(1) within setXY, and then, being preempted by another one executing setY(2) (also within setXY). Now before the first thread recovers execution check is called because check and setXY are not synchronized.
C. Check() can return true when multiple threads call setX and setY separately.
True just imagine the sequence setX(1); setY(2); check();
For being false both calls must occur in the same synchronized method, and this must be synchronized with check on the same lock.
D. Check() can only return true if SyncTest is changed to allow x and y to be set separately.
False. Being setXY not synchronized is possible that check return true in many ways regarding the order of execution of the three methods that modify the variables.

 
Hades Pan
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
New quession
1)
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();
new Thread() {
public void run(){
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() {
synchronized(this) {
h.sb1.append("C");
h.sb2.append("D");
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.

####Same type of question with little change
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();
new Thread() {
public void run(){
synchronized(h.sb1) {
h.sb1.append("A");
h.sb2.append("B");
System.out.println(h.sb1);
System.out.println(h.sb2);
}
}
}.start();
new Thread() {
public void run() {
synchronized(h.sb1) {
h.sb1.append("C");
h.sb2.append("D");
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.
[This message has been edited by Hades Pan (edited December 05, 2001).]
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Hades
for the first code:
This the output
"
A
BD
BD
AC
"
for this execution you can see how the sentences:
"
h.sb1.append("A");
h.sb2.append("B");
System.out.println(h.sb1);
"
are executed first, then the first thread is preempted by the second and these ones are executed:
"
h.sb1.append("C");
h.sb2.append("D");
System.out.println(h.sb2);// (*)
"
then the sencond thread is preempted by the first and executes:
"
System.out.println(h.sb2);// (*)
"
and lastly:
"
System.out.println(h.sb1);
"
(*) you can not say the exact order of these senteces as the code is, but it is irrelevant for the discussion
This is so because the threads has not been locked on the same object. Both has been locked on itself. Here there is no deadlock because these threads are not synchronized to each other.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for the secod code:
Both threads are now synchronized on the same object. There is no deadlock because once a thread has the lock is doesn't have to wait for a condition that can not be satisfied.
Supposing the first tread starts first the output is:
"
A
B
BD
AC
"
Supposing the other thread begins first:
D
C
CA
DB
this output was not obtained through an execution
Now the printings of both threads are not mixed.
 
Hades Pan
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I run two cord of above the same end "A B BD AC"on my computer (JDK 1.3)
What's the matter on it?
I am very anxiety on this Quession becaues I will take the SCJP EXAM tomorrow morning.
 
Hades Pan
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this the same result with cord 2 above?
class Hppp extends Thread {
final static StringBuffer sb1 = new StringBuffer();
final static StringBuffer sb2 = new StringBuffer();
public static void main(String args[]) {
new Thread() {
public void run() {
synchronized(sb1) {
sb1.append("A");
sb2.append("B");
System.out.println(sb1);
System.out.println(sb2);
}
}
}.start();
new Thread() {
public void run() {
synchronized(sb2) {
sb1.append("C");
sb2.append("D");
System.out.println(sb2);
System.out.println(sb1);
}
}
}.start();
}
}
Or will Output non-deterministic because of the chance of dadlock?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry my English is not good enough to understand the word "cord" in this context.
This is what I can tell you:
If the two threads are synchronized on the same object. The execution of one can not procede untill the other has ended.
These are the outputs for such situations:
"
Supposing the first tread starts first the output is:
A
B
BD
AC
Supposing the other thread begins first:
D
C
CA
DB
"
If they were not synchronized, or they were synchronized on different objects, the execution of the threads could be mixed producing something like:
"
A
BD
BD
AC
"
A deadlock situation can occur if each thread is waiting for the other to release the lock. Look at this example:


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. }


Suppose thread one executes line 8, here it has the lock of s1. Now the scheduler decides to execute thread two, which will progress untill line 21 and blocks because that lock is hold by the other thread. Note that at this point thread two has the lock s2 but it is blocked waiting for thread one to release s1. When the sheduler decides to give a time slice to thread one, it will resume execution at line 9. There, it will block because s2 was hold by thread two who is waiting for thread one to release s1. But thread one can not release s1 because is blocked waiting for thread two to release s2. Now both threads are blocked waiting for each other. This a deadlock. To avoid this (esily spotted) deadlock just make certain that when adquiring several locks (s1 and s2) they are adquired in the same sequence:
Modify the previous program replacing line 19 for "synchronized(s1)" and line 21 for "synchronized(s2)"

I don't know the answer for f)
why don't you post it in Threads forum here and in Sun site?The point is: if the example is properly synchronized we can expect that the behaviour of the program is not system dependant.
Do you agree?
 
Hades Pan
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Jose Botella very very much!! You are a hero in JAVA world!
 
reply
    Bookmark Topic Watch Topic
  • New Topic