About Question enthuware.ocpjp.v7.2.1341 :
Moderator: admin
-
- Posts: 5
- Joined: Wed Mar 27, 2013 4:14 am
- Contact:
About Question enthuware.ocpjp.v7.2.1341 :
A think that answer is wrong because in this question created two instance and
each has own variables.
each has own variables.
-
- Posts: 5
- Joined: Wed Mar 27, 2013 4:14 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1341 :
Sorry, a get it) (i missed that variables are static).riverbox wrote:A think that answer is wrong because in this question created two instance and
each has own variables.
-
- Posts: 17
- Joined: Fri Sep 06, 2013 1:37 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1341 :
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();
?
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();
?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1341 :
Why do you think so? run() method is not synchronized. Please go through the explanation.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?It seems to me that the last invocation of testXY() will always print false.
You are right. synchronized doesn't help here. The setX and setY methods should actually be static but that will still not help.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.
No. In what sense?Are these locks affect each other?
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.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();
?
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 17
- Joined: Fri Sep 06, 2013 1:37 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1341 :
Thank you, Paul, for your explanation!
-
- Posts: 78
- Joined: Sun Jun 30, 2013 10:04 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1341 :
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.
------
Question: How can one understand this anomaly?
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
*/
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1341 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 30
- Joined: Sat May 02, 2015 4:48 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1341 :
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?
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?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1341 :
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.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?
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Sun Oct 04, 2015 8:54 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1341 :
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?
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?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1341 :
That is correct. If you make the run method it will always print false false.
If you like our products and services, please help us by posting your review here.
-
- Posts: 97
- Joined: Wed Dec 28, 2016 9:00 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1341 :
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?
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?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1341 :
That is correct.
If you like our products and services, please help us by posting your review here.
-
- Posts: 97
- Joined: Wed Dec 28, 2016 9:00 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1341 :
Thanks for quick reply!admin wrote:That is correct.
-
- Posts: 3
- Joined: Wed Apr 05, 2017 1:33 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1341 :
I ran the code and it always print:
false
false
so I'm not sure what is wrong. Here's my code:
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();
}
}
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1341 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 59 guests