Page 1 of 1

About Question enthuware.ocpjp.v7.2.1249 :

Posted: Fri Mar 28, 2014 4:33 pm
by tn1408
Hello,
Good question, I followed your explanation and ran the code.
In your explanation, there are 2 threads running. Both print before any update (incrementing) is done. Let's continue where your explanation left off:

Thread1 prints, interrupted, thread2 prints, increment i, interrupted
i = 1
Assume thread 1 (was interrupted) now gets to run, will it continue what it was doing: which is updating i first? So i = 2, now it goes back to the for loop...Obviously it's not doing this. Otherwise we would have i = 2, and 2 prints and the answer would be: Total 5 words printed.

In other words: when the thread wakes up to run, where exactly in the execution path does it start from?
Thanks,

Tony,

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

Posted: Fri Mar 28, 2014 9:50 pm
by admin
A thread will continue right after the statement that it executed last. So yes, it will update i but the problem is with the statement i++. This statement is not atomic and is actually composed of three steps -
S1. read the current value of i.
S2. Increment the read value by 1.
S3. Store the incremented value back in i.

So now,
at S1, Thread 1 gets 0.
at S2, it increments it to 1,
(Gets interrupted and another thread increments i to 1).
Thread 1 wakes up and goes to execute S3, where it assigns the result it got at S2 i.e. 1 to i. So instead of i becoming 2, it remained at 1.

HTH,
Paul.

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

Posted: Sat Mar 29, 2014 2:07 am
by tn1408
I see
Thanks,

Tony

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

Posted: Wed Aug 19, 2015 5:16 am
by colmkav
Can it also print fewer than 5 numbers? I think so because they may increment i and the same time and then one of the thread finishes before the other has a chance to print anything more.

Are there any upper and lower limits to the number that can be printed?

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

Posted: Mon Nov 07, 2016 12:47 am
by Sai Divya sree
Since i is not volatile in this case ,the i value incremented by one thread may not be visible to another.Is it wrong to think that way in this question?So Hello will be printed five times and world will be printed five times although not in the serial order.

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

Posted: Mon Nov 07, 2016 3:31 am
by admin
Sai Divya sree wrote:Since i is not volatile in this case ,the i value incremented by one thread may not be visible to another.Is it wrong to think that way in this question?So Hello will be printed five times and world will be printed five times although not in the serial order.
It is true that i is not volatile. But you cannot assume that its updated value will not be visible at all to other threads. It may be visible or it may not be visible. There is no guarantee. What if there is only one CPU core?

HTH,
Paul.

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

Posted: Fri Jan 20, 2017 6:00 am
by jagoneye
Can hello world be printed less than five times in this case?