Page 1 of 1

About Question enthuware.ocpjp.v8.2.1078 :

Posted: Sun Jul 31, 2016 12:28 pm
by sharpmario
Hi, the question has the follow options:
0 1
1 1
0 0
1 2
and the right answer is "0 1" and "0 0" but, if "0 0" is correct, should not "1 1" be correct as well? The explanation states it, taking in consideration that is not known the value of the variable before the execution.

Thanks

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

Posted: Sun Jul 31, 2016 8:03 pm
by admin
No, 1 1 is not possible because the initial value is count is 0. Observe that count is defined as private int count; so it will be initialized to the default value of 0. So the thread that executes print first will print 0.

HTH,
Paul.

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

Posted: Wed Aug 03, 2016 6:38 am
by sharpmario
Ok, thanks for your help! :thumbup:

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

Posted: Fri May 11, 2018 7:33 am
by Harvey Manfrenjensen
Hi, I tried to reproduce the result of this question
(please see the code below), but i get
0 1 and 1 0 as result, but 1 0 is not the possible choice.
What could be wrong?
Regards, Paul

Code: Select all

public class enthuware_1078 {
    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++){
            System.out.print("i = " + i + "\t");
            MyCounter c = new MyCounter();
            MyRunnable mr = new MyRunnable(c);
            Thread t1 = new Thread(mr);
            Thread t2 = new Thread(mr);
            t1.start();
            t2.start();
            try {
                t1.join();
                t2.join();
            } catch (InterruptedException ex) {
            }
            System.out.println();
        }
    }
}

class MyRunnable implements Runnable{
    private MyCounter counter;
    MyRunnable(MyCounter counter){
        this.counter = counter;
    }
            
    @Override
    public void run() {
        counter.increment();
    }
}

class MyCounter {
    private int count;
    public void increment(){
        System.out.print(count++);
    }
}

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

Posted: Fri May 11, 2018 10:08 am
by admin
There is nothing wrong. 1 0 is just not one of the options. That doesn't mean it is not a valid output. The question is only asking you to select possible outputs out of the ones that it has given. It does not say that it has listed all possible outputs.

HTH,
Paul.

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

Posted: Sat May 12, 2018 6:59 am
by Harvey Manfrenjensen
admin wrote:There is nothing wrong. 1 0 is just not one of the options. That doesn't mean it is not a valid output. The question is only asking you to select possible outputs out of the ones that it has given. It does not say that it has listed all possible outputs.

HTH,
Paul.
Hello Paul, thank you :idea:

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

Posted: Thu Jul 26, 2018 1:31 am
by ArpRokz
I understand that 0 1 and 0 0 are possible outputs if a thread accesses the count variable before the other thread has completely updated its value but option 1 2 also seems to be a correct answer to me. count++ is not an atomic operation and may produce unexpected results but it can be a case where the second thread actually access count after it has been updated by the first thread then 1 2 is correct option.

I mean to say there is a chance of 0 0 or 0 1 since the operation ++ is not atomic but there can also be a case where everything goes as expected and outputs 1 2 . :?

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

Posted: Thu Jul 26, 2018 2:03 am
by admin
1 2 is not a possible output because the code is printing count++ (and not ++count). So if everything goes as expected the output will be 0 1.

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

Posted: Thu Jul 26, 2018 2:56 am
by ArpRokz
Yes you are correct I didn't notice that. Thanks for the quick reply :)

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

Posted: Thu Dec 26, 2019 9:16 am
by Bhaskar
admin wrote:
Fri May 11, 2018 10:08 am
There is nothing wrong. 1 0 is just not one of the options. That doesn't mean it is not a valid output. The question is only asking you to select possible outputs out of the ones that it has given. It does not say that it has listed all possible outputs.

HTH,
Paul.
I am trying to understand why is 1 0 a valid output. Post-increment will first print the value of count and then increment it. So no matter which thread runs, 0 will always be printed first followed by further processing. Am i wrong in my understanding of post increment here?

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

Posted: Thu Dec 26, 2019 9:31 am
by admin
count++ and the print operation are not atomic. It is possible that one thread passes the initial value of 0 to println, increments count to 1 but before it is able to print the value 0, the second thread gets to run, passes the new value 1 to println, increments count to 2, prints the value 1. The first thread now gets to print the value that it was passed to print i.e. 0.

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

Posted: Thu Dec 26, 2019 9:40 am
by Bhaskar
admin wrote:
Thu Dec 26, 2019 9:31 am
count++ and the print operation are not atomic. It is possible that one thread passes the initial value of 0 to println, increments count to 1 but before it is able to print the value 0, the second thread gets to run, passes the new value 1 to println, increments count to 2, prints the value 1. The first thread now gets to print the value that it was passed to print i.e. 0.
Thank you for the quick reply. Appreciate it.