"The line id = 1 - id; swaps of id between 0 and 1. There is no chance for the same method to be executed twice."
OCPJP 6 96%
micha koern wrote:Wouldnt this mean, that RSRS is printed?
That would be wrong if my theory is correct."The line id = 1 - id; swaps of id between 0 and 1. There is no chance for the same method to be executed twice."
OCPJP 6 96%
My reasoning is as follows: the first thread can execute "id = 1 - id;" so that id == 0 now, AND BEFORE it executes the next line (if (id == 0) { pick(); }) the second thread could execute "id = 1 - id;" so id would be id == 1 again
anirudh jagithyala wrote:
My reasoning is as follows: the first thread can execute "id = 1 - id;" so that id == 0 now, AND BEFORE it executes the next line (if (id == 0) { pick(); }) the second thread could execute "id = 1 - id;" so id would be id == 1 again
i think that
by the time second thread executes id=1-id the first thread would move to the next line (if (id == 0) { pick(); })
as the methods are not synchronised the threads would execute simultaneously and not wait for other thread .....
May this would help
OCPJP 6 96%
There is no chance for the same method to be executed twice
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() .
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.
OCPJP 6 96%
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.
OCPJP 6 96%
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.
OCPJP 6 96%
OCPJP 6 96%
Francisco J. Bermejo wrote:By the way, I'd like to know the authors' opinion to know if in the real exam I should be so picky
. Bert?
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. ;)
OCPJP 6 96%
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Bert Bates wrote:Great discussion you guys!
This one is gonna cause me to go find my "threads" hat to put on...
I'm leaning towards thinking that there's a problem with the question, but I haven't looked at it closely yet.
Thanks for spotting this one!
Just let me do the talking. Ahem ... so ... you see ... we have this tiny ad...
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|