About Question enthuware.ocpjp.v7.2.1249 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
tn1408
Posts: 28
Joined: Wed Dec 04, 2013 7:57 pm
Contact:

About Question enthuware.ocpjp.v7.2.1249 :

Post 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,

admin
Site Admin
Posts: 10057
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

tn1408
Posts: 28
Joined: Wed Dec 04, 2013 7:57 pm
Contact:

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

Post by tn1408 »

I see
Thanks,

Tony

colmkav
Posts: 21
Joined: Thu Jul 16, 2015 4:22 am
Contact:

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

Post 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?

Sai Divya sree
Posts: 14
Joined: Mon Jun 20, 2016 11:16 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 10057
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

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

Post by jagoneye »

Can hello world be printed less than five times in this case?

Post Reply

Who is online

Users browsing this forum: No registered users and 37 guests