SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
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
SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
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.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
SCJP6
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.
SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
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.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
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.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Vijitha Kumara wrote:I don't think that would still work? They are synchronizing on two instances.
SCJP6
Well THAT's new! Comfort me, reliable tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|