| Author |
Enthuware question doubt
|
Sonali Sehgal
Ranch Hand
Joined: Jul 09, 2009
Posts: 75
|
|
This is an Enthuware question .
The above code will always print "false".???
Option: true or false
Answer:True
In a real time scenario the program output is false and false again
|
 |
Vijitha Kumara
Bartender
Joined: Mar 24, 2008
Posts: 3673
|
|
|
These are two different threads which run independently. But if one thread gets a chance and call setXY(1,2) (note that those values have no effect on the x,y) it then calls only setX() after that the next thread gets a chance and completes it's run method. Then x --> 2 and y --> 1. So I think true also possible as output.
|
SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9191
|
|
This is an error in enthuware mock exam. It has been asked before many times
http://www.coderanch.com/t/264707/Programmer-Certification-SCJP/certification/Thread
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Lucas Smith
Ranch Hand
Joined: Apr 20, 2009
Posts: 804
|
|
Sonali Sehgal wrote:
This is an Enthuware question .
The above code will always print "false".???
Option: true or false
Answer:True
In a real time scenario the program output is false and false again
One keyword was added by me and that code will always print false false.
Correct me if I am wrong.
|
SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
|
 |
Sonali Sehgal
Ranch Hand
Joined: Jul 09, 2009
Posts: 75
|
|
Hi Lukas,
The answer of the question is already coming as falsefalse
My question was that the entuware says as this is not the right output so I highlighted it on the forum...
@Ankit
Thanks a lot
I do agree the question is wrong.However if this question comes in SCJP exam then what is the right output.
|
 |
Lucas Smith
Ranch Hand
Joined: Apr 20, 2009
Posts: 804
|
|
Usually false false but it is not guaranteed
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9191
|
|
@Sonali, if the question comes in SCJP, then the answer is that the ouput can be true false or false false. The second output can never be true.
@Lukas, I still feel that there is a tiny possibility that the output can be true even after your change (although I'm not sure). Since testXY is not synchronized, there is a chance that the value of x is incremented by the second thread and before the second thread can increment the value of y, testXY is called by the first thread. So the expression x != y will evaluate to 2 != 1 thus true...
|
 |
Paul Anilprem
Enthuware Software Support
Ranch Hand
Joined: Sep 23, 2000
Posts: 2922
|
|
Sonali,
Can you please quote the QuestionId so that I can look into the question and investigate further?
Ankit,
The code that you have referenced from another thread is different from the one that Sonali has mentioned here. I couldn't find that question in our question bank either.
HTH,
Paul.
|
Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9191
|
|
|
Yes, its my fault. This question looked very similar to the one in that other thread and I didn't see it closely...
|
 |
Paul Anilprem
Enthuware Software Support
Ranch Hand
Joined: Sep 23, 2000
Posts: 2922
|
|
Ok, I managed to track down this question. Its QuestionId is com.enthuware.ets.scjp.v6.2.174 and the question is :
The answer that is given is "False", which is right.
It also contains the following explanation:
You are creating 2 threads in main().
Consider this scenerio:
thread 1, calls setXY, in setXY it calls setX() (after getting the lock) and sets x to 1. Now setX ends and the lock is released. Before it could again get the lock to call setY(), the OS runs thread 2. The thread 2 again increments x to 2 and then it calls setY thereby incrementing y to 1. Now, it calls testXY and x is 2 and y is 1 so it will return true!
The point to note here is, calling two synchronized methods one after another from a non-synchronized method does not guarantee the order of updation of shared data.
HTH,
Paul.
|
 |
Jason Irwin
Ranch Hand
Joined: Jun 09, 2009
Posts: 327
|
|
The Enthuware explanation is quite correct, although it may be very unlikely to happen it is still possible that the output is "true".
Lukas's addition is also correct. Making testXY synchronised means that there is no "break" in the locking between the calls to setX and setY any more and thus no chance for another thread to interfere.
|
SCJP6
|
 |
Vijitha Kumara
Bartender
Joined: Mar 24, 2008
Posts: 3673
|
|
Lukas's addition is also correct. Making testXY synchronised means that there is no "break" in the locking between the calls to setX and setY any more and thus no chance for another thread to interfere.
I don't think that would still work? They are synchronizing on two instances.
|
 |
Sonali Sehgal
Ranch Hand
Joined: Jul 09, 2009
Posts: 75
|
|
Hi Paul,
The code of the enthuware question is:-
com.enthuware.ets.scjp.v5.2.174
The question id is different from the one you mentioned.
|
 |
Paul Anilprem
Enthuware Software Support
Ranch Hand
Joined: Sep 23, 2000
Posts: 2922
|
|
Sonali Sehgal wrote:Hi Paul,
The code of the enthuware question is:-
com.enthuware.ets.scjp.v5.2.174
The question id is different from the one you mentioned.
Hi Sonali,
Thanks for reporting the QID. The id you mentioned is from an older version but the question, its answer, and its explanation are same. Are you seeing any difference in that?
HTH,
Paul.
|
 |
Sonali Sehgal
Ranch Hand
Joined: Jul 09, 2009
Posts: 75
|
|
Hi Paul,
As far as I understand the answer is wrong and in the explanation part the output part is wrong.Henceforth, question is correct and answer iscoming in the software but answer in enthuware is not correct.
|
 |
Paul Anilprem
Enthuware Software Support
Ranch Hand
Joined: Sep 23, 2000
Posts: 2922
|
|
Sonali Sehgal wrote:Hi Paul,
As far as I understand the answer is wrong and in the explanation part the output part is wrong.Henceforth, question is correct and answer iscoming in the software but answer in enthuware is not correct.
Hi,
I am sorry but I am not sure what is the confusion. I am looking at both the versions and I see that in both the versions, the given answer and its explanation are correct. I am not sure what you mean by "answer is coming in software but answer in enthuware is not correct."
|
 |
S. Vijay
Ranch Hand
Joined: Jul 10, 2009
Posts: 44
|
|
Hi everyone
I feel the question and solution are correct in enthuware. Correct me if i am wrong
Actually in the code there are 2 different thread objects initiating start method so there are two different thread objects and also the methods setXY and testXY are not synchronized so there is a chance of second thread to access setX method immediately after first thread sets X value.
I have tried with the sleep method inside synchronized method setX() and I got the output as true,false. As Thread.sleep sleeps the current thread for specific amount of time but without releasing the lock. Since there are 2 different thread objects the second thread can enter the setX method.
Also I got output true false when I made a minor change.
I added in setX() method and in setY() method. I think this console printing gives enough time for the other thread to access the same method.
I got the output as
Thread-0 X: 2
Thread-0 Y: 1
true
Thread-1 X: 2
Thread-1 Y: 2
false
If this is not the case then the synchronized method setX() should not allow other thread to enter right??? In the above output I found out that when thread 0 entered the setX method it got incremented to 1 and immediately the control switched to thread 1 and again the x value got incremented to 2 and hence in both the methods the value of X is 2. This can be possible since three were 2 different thread objects and even though presence of synchronized keyword it doesn't stop different threads in accessing the same method.
Please correct me if my explanation is wrong.
Regards
Vijay
|
 |
Jason Irwin
Ranch Hand
Joined: Jun 09, 2009
Posts: 327
|
|
Vijitha Kumara wrote:I don't think that would still work? They are synchronizing on two instances.
You're right. It's been a long day.....I'm going home to do some mocks.
|
 |
 |
|
|
subject: Enthuware question doubt
|
|
|