Page 1 of 1
About Question enthuware.ocpjp.v7.2.1341 :
Posted: Wed May 29, 2013 2:12 am
by riverbox
A think that answer is wrong because in this question created two instance and
each has own variables.
Re: About Question enthuware.ocpjp.v7.2.1341 :
Posted: Wed May 29, 2013 2:29 am
by riverbox
riverbox wrote:A think that answer is wrong because in this question created two instance and
each has own variables.
Sorry, a get it) (i missed that variables are static).
Re: About Question enthuware.ocpjp.v7.2.1341 :
Posted: Mon Mar 17, 2014 1:11 am
by M.Komarov
Hi!
1. The final state of static members of class Test (regardless of the order of updation of shared data) is x=2, y=2. Instructions in the body of run() acts in happens-before relationship. It seems to me that the last invocation of testXY() will always print false.
2. It seems to me also that synchronized keyword is redundant in this context. Methods setX and setY are not static and we create 2 instances of class Test. Thus each thread enters its own exclusive lock. Are these locks affect each other? In order to "switch on" synchronized keyword is it possible to rewrite the main method like this
Test t = new Test();
new Thread(t).start();
new Thread(t).start();
?
Re: About Question enthuware.ocpjp.v7.2.1341 :
Posted: Mon Mar 17, 2014 1:25 am
by admin
M.Komarov wrote:Hi!
1. The final state of static members of class Test (regardless of the order of updation of shared data) is x=2, y=2. Instructions in the body of run() acts in happens-before relationship.
Why do you think so? run() method is not synchronized. Please go through the explanation.
It seems to me that the last invocation of testXY() will always print false.
Why?
2. It seems to me also that synchronized keyword is redundant in this context. Methods setX and setY are not static and we create 2 instances of class Test. Thus each thread enters its own exclusive lock.
You are right. synchronized doesn't help here. The setX and setY methods should actually be static but that will still not help.
Are these locks affect each other?
No. In what sense?
In order to "switch on" synchronized keyword is it possible to rewrite the main method like this
Test t = new Test();
new Thread(t).start();
new Thread(t).start();
?
Yes, but it will still not help because setXY itself is not synchronized. Calling two synchronized methods one after another does not ensure atomicity of the caller method. Please see the scenario given in the explanation.
HTH,
Paul.
Re: About Question enthuware.ocpjp.v7.2.1341 :
Posted: Tue Mar 18, 2014 2:29 pm
by M.Komarov
Thank you, Paul, for your explanation!
Re: About Question enthuware.ocpjp.v7.2.1341 :
Posted: Tue Jan 27, 2015 2:57 pm
by ewebxml
The question states:
The above code will always print false.
a. True
b. False:
When I ran the code below it always printed "false".
I would say the answer is a) true but the question is saying the answer is b) false.
------
Code: Select all
public class Test extends Thread{
/* Variables are static */
static int x, y;
public synchronized void setX(int i) { x++; }
public synchronized void setY(int j) {y++; }
public void setXY(int i, int j) { setX(i); setY(j); }
public boolean testXY() {
System.out.println("x = " + x + ",y= " + y);
return x != y;
}
public void run(){
setXY(1, 2);
System.out.println(testXY()); }
public static void main(String[] args){
new Test().start();
new Test().start();
}
}
/*
Program Output
x = 1,y= 1
false
x = 2,y= 2
false
*/
Question: How can one understand this anomaly?
Re: About Question enthuware.ocpjp.v7.2.1341 :
Posted: Tue Jan 27, 2015 9:30 pm
by admin
Please see my post above. Just because you are getting the same answer all the time doesn't mean that is what will happen all the time for every one.
Re: About Question enthuware.ocpjp.v7.2.1341 :
Posted: Mon Sep 14, 2015 12:26 pm
by Danny Sheridan
Maybe a bit of a moot point but as well as the order not being guaranteed here,
one thread may not see changes made by the other as x and y are not marked as volatile
and this too could in theory lead to true being returned?
Re: About Question enthuware.ocpjp.v7.2.1341 :
Posted: Mon Sep 14, 2015 8:45 pm
by admin
Danny Sheridan wrote:Maybe a bit of a moot point but as well as the order not being guaranteed here,
one thread may not see changes made by the other as x and y are not marked as volatile
and this too could in theory lead to true being returned?
No, that is not correct. Java memory model guarantees that if two threads synchronize on the same object then updates made by one thread after synchronization will definitely be visible immediately to the other after the other thread acquires the same lock. volatile is not necessary here.
Volatile makes the changes visible to any other thread without any synchronization. The problem with volatile is that the timing is not guaranteed. For example, if one thread updates a volatile variable, the other thread will see the updates but may not see them immediately. volatile is a cheaper (but less deterministic) alternative to synchronized.
You should read more about this to get a clear understanding.
HTH,
Paul.
Re: About Question enthuware.ocpjp.v7.2.1341 :
Posted: Thu May 26, 2016 2:23 am
by johnwonlim
Hi,
when you make the 'setXY' method synchronized (and leave everything else the same in the code), will it always print 'false' 'false'?
I don't think so because of following scenario:
-Thread 1 finishes executing 'setXY(1,2)' and releases the lock => x=1, y=1
-Before thread 1 executes 'System.out.println(testXY())', thread 2 acquires the lock on 'setXY(1,2)' and executes setX(i) => x=2,y=1
-thread 1 finishes executing 'System.out.println(testXY())' and prints 'true'
Thus, I think you need to make 'run()' method synchronized in order to gaurantee 'false' 'false'. Is this correct?
Re: About Question enthuware.ocpjp.v7.2.1341 :
Posted: Thu May 26, 2016 5:10 am
by admin
That is correct. If you make the run method it will always print false false.
Re: About Question enthuware.ocpjp.v7.2.1341 :
Posted: Wed Jan 11, 2017 6:18 am
by jagoneye
Firstly the variables are static hence even if you synchronize the instance methods still they will be accessible by other threads. To lock access to those variables we need to either synchronize
the Test.class or declare those synchronized methods as static.
Secondly if those variables were non static still each would have their own instance since two threads are operating on different thread objects. So in those cases the output would always be
False False. I hope I'm right?
Re: About Question enthuware.ocpjp.v7.2.1341 :
Posted: Wed Jan 11, 2017 11:58 am
by admin
That is correct.
Re: About Question enthuware.ocpjp.v7.2.1341 :
Posted: Wed Jan 11, 2017 12:00 pm
by jagoneye
admin wrote:That is correct.
Thanks for quick reply!

Re: About Question enthuware.ocpjp.v7.2.1341 :
Posted: Wed Apr 05, 2017 1:38 pm
by javaace
I ran the code and it always print:
false
false
so I'm not sure what is wrong. Here's my code:
Code: Select all
public class Test extends Thread{
static int x, y;
public static synchronized void setX(int i){ x++;}
public static synchronized void setY(int j){ y++;}
public void setXY(int i, int j){
setX(i);
setY(j);
}
public boolean testXY(){
return x != y;
}
public void run(){
setXY(1,2);
System.out.println(testXY());
}
public static void main(String[] args) {
new Test().start();
new Test().start();
}
}
Re: About Question enthuware.ocpjp.v7.2.1341 :
Posted: Wed Apr 05, 2017 8:47 pm
by admin
There is nothing wrong. Did you read the scenario described in the explanation? It will not print false false in that situation. Although rare, but possible.
HTH,
Paul.