About Question enthuware.ocpjp.v7.2.1078 :

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

Moderator: admin

Post Reply
shareef.hiasat
Posts: 20
Joined: Thu Dec 19, 2013 8:22 am
Contact:

About Question enthuware.ocpjp.v7.2.1078 :

Post by shareef.hiasat »

Why isn`t 1 1 a possible solution ; i read the explanation but i want super slow motion to understand this

thanks
This is a straight forward implementation on an thread unsafe class. Observe that count is a shared resource that is accessed by multiple threads and there are multiple issues in this code:

1. Since access to count is not synchronized, there is no guarantee that changes made by thread 1 will even be visible to thread 2. Thus, both the threads may print 0 and increment it to 1 even if they run one after the other. To understand this point, you need to read about topic of visibility guarantee provided by the Java memory model.

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

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

Post by admin »

Here is the super slow version :) http://java.dzone.com/articles/multithr ... ava-memory
and here is another good article explaining the whole thing: https://www.securecoding.cert.org/confl ... and+Memory
If you like our products and services, please help us by posting your review here.

shareef.hiasat
Posts: 20
Joined: Thu Dec 19, 2013 8:22 am
Contact:

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

Post by shareef.hiasat »

admin wrote:Here is the super slow version :) http://java.dzone.com/articles/multithr ... ava-memory
and here is another good article explaining the whole thing: https://www.securecoding.cert.org/confl ... and+Memory

many thanks for the links

but my second question is 0 0 even possible solution ?! and why

Consider the following class:

Code: Select all

public class Counter {
    private int count;
    public void increment(){
        System.out.println(count++);
    }
 }
If two threads call the increment() method on the same Counter instance simultaneously, which of the following are possible outputs? (Assume that there are no other calls to the Counter instance.)

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

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

Post by admin »

If you like our products and services, please help us by posting your review here.

goodness
Posts: 2
Joined: Mon Nov 10, 2014 6:14 pm
Contact:

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

Post by goodness »

Scenario 1
Thread 1: Retrieve count. Print count; output is 0.
Thread 2: Retrieve count. Print count; output is 0.
Thread 1: Increment retrieved value; result is 1.
Thread 2: Increment retrieved value; result is 1.
Thread 1: Store result in count; count is now 1.
Thread 2: Store result in count; count is now 1.

Scenario 2
Thread 1: Retrieve count. Print count; output is 0.
Thread 1: Increment retrieved value; result is 1.
Thread 2: Retrieve count. Print count; output is 0.
Thread 1: Store result in count; count is now 1.
Thread 2: Increment retrieved value; result is 1.
Thread 2: Store result in count; count is now 1.

Scenario 3
Thread 1: Retrieve count. Print count; output is 0.
Thread 1: Increment retrieved value; result is 1.
Thread 1: Store result in count; count is now 1.
Thread 2: Retrieve count. Print count; output is 1.
Thread 2: Increment retrieved value; result is 2.
Thread 2: Store result in count; count is now 2.

sir_Anduin@yahoo.de
Posts: 62
Joined: Fri Aug 07, 2015 2:16 pm
Contact:

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

Post by sir_Anduin@yahoo.de »

Hi,
I still cant understand how one result might be 0 as in any case the final asignment to count will be at least 1:
count = 0
temp = count +1 (temp =1)
count = temp.

so count must be at leat 1.

but as i tried it, the result is as you say...

rohitbe
Posts: 5
Joined: Fri Mar 10, 2017 12:25 pm
Contact:

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

Post by rohitbe »

I would like to add to this.
The output would be option B and D i.e 1 1 and 1 2 if and only if count is pre-incremented
public void increment(){
System.out.println(++count);
}

Correct me, if I am wrong

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

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

Post by admin »

Yes, in case of pre increment, the output would be either 1 1 or 1 2.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: admin and 40 guests