Andreas Svenkson wrote:
EDIT: Then again, I find it odd that there would be a question on something that isn't guaranteed bevhaviour in the first place... Francisco, could you post the entire question please?
Paul Clapham wrote:Let's suppose, just as a hypothesis, that when you compare an int to a float, the int is first converted to a float. Wouldn't that make your posted code behave exactly as it does behave?
Conversion of an int or a long value to float, or of a long value to double, may result in loss of precision-that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode (§4.2.4).
Stephan van Hulst wrote:The object referenced by c2 is still reachable (through c2).
Francisco J. Bermejo wrote:Until now I simply assumed (wrongly) that in this kind of questions the garbage collector did not run until the asked line is reached.
"Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible."
Roel De Nijs wrote:
It's true that a total of 5 objects are created, but when reaching line 5 just 4 will be eligible for garbage collection (because 1 of the created objects became already eligible on another time, so this object could already be garbage collected when reaching line 5).
Stephan van Hulst wrote:I think it should be theoretically possible for id == 0 to hold true twice, because id = 1 - id is not an atomic operation, and id is also not volatile. I guess in theory P Q P Q should be a possible outcome. Let's see if someone else can comment on this.
Mark Spritzler wrote:As someone who wrote questions for the 5.0 exam. Yes, the Thread questions are just like this one. And you really have to be picky and make sure you check everything that could possibly happen. Or you could do what I did and just guess on those darn Thread questions. ;)
micha koern wrote:Oh Right!
The output is false, so there is really a new Object- i agree 5 Objects are eligible.
Hareendra Reddy wrote:Hello all,
so id would be id == 1 again (remember almost nothing in Java threads is guaranteed, and so is the order of execution of threads). Therefore both threads would execute the method pick() .
Your reasoning is right but when the value of id is 1 then the else block is executed printing "RSRS".I feel that P Q P Q is not possible because id can't be made to zero for both the threads.
Dennis Deems wrote:I have run this code several times. I never got RSRS, but a few times I did get RSPQ, and once RPQS. I do not think P Q P Q or RSRS are possible.
That explanation is simply wrong according to my reasoning.
The line id = 1 - id; swaps of id between 0 and 1. There is no chance for the same method to be executed twice.
micha koern wrote:
There is no chance for the same method to be executed twice
This refers to the synchronized methods that are not responsible for your problem.