About Question enthuware.ocpjp.v7.2.1341 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
riverbox
Posts: 5
Joined: Wed Mar 27, 2013 4:14 am
Contact:

About Question enthuware.ocpjp.v7.2.1341 :

Post by riverbox »

A think that answer is wrong because in this question created two instance and
each has own variables.

riverbox
Posts: 5
Joined: Wed Mar 27, 2013 4:14 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1341 :

Post 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).

M.Komarov
Posts: 17
Joined: Fri Sep 06, 2013 1:37 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1341 :

Post 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();

?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1341 :

Post 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.
If you like our products and services, please help us by posting your review here.

M.Komarov
Posts: 17
Joined: Fri Sep 06, 2013 1:37 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1341 :

Post by M.Komarov »

Thank you, Paul, for your explanation!

ewebxml
Posts: 78
Joined: Sun Jun 30, 2013 10:04 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1341 :

Post 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?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1341 :

Post 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.
If you like our products and services, please help us by posting your review here.

Danny Sheridan
Posts: 30
Joined: Sat May 02, 2015 4:48 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1341 :

Post 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?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1341 :

Post 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.
If you like our products and services, please help us by posting your review here.

johnwonlim
Posts: 1
Joined: Sun Oct 04, 2015 8:54 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1341 :

Post 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?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1341 :

Post by admin »

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.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1341 :

Post 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?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1341 :

Post by admin »

That is correct.
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1341 :

Post by jagoneye »

admin wrote:That is correct.
Thanks for quick reply! :D

javaace
Posts: 3
Joined: Wed Apr 05, 2017 1:33 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1341 :

Post 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();

	}
}

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1341 :

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 59 guests