Page 1 of 1

About Question enthuware.ocpjp.v8.2.1391 :

Posted: Fri Jan 13, 2017 12:32 pm
by sandeshchaudhari
When i run this program it prints nothing in the o/p but answer given is 0 2 4. Can you please tell me why this?

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

Posted: Sat Jan 14, 2017 12:14 am
by admin
Did you read the explanation? It explains exactly what you are asking.

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

Posted: Wed Feb 22, 2017 4:42 am
by runnerdave
Can you please explain why the results are always ordered?

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

Posted: Wed Feb 22, 2017 5:17 am
by admin
The main thread prints the value of data variable of each thread one by one in the for loop. So irrespective of what the value of data variable is for any thread, the first value printed will be the data value of the first thread, second value printed will be the data value of the second thread, and the third value printed will be the data value of the third thread.

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

Posted: Wed May 31, 2017 12:56 am
by shatterblast
Will you please explain why the output is always in increments of 2?

I try the following code, and the output comes out as 0 2 4 6 8. Thank you for your time.

Code: Select all

    ...  //Other code.
    
    public static void main(String[] args) throws Exception
    {
        Test[] jta = new Test[5];
        for(int i=0; i<5; i++) {
            jta[i] = new Test(i); jta[i].start();
        }
        for(Test jt : jta) {
            if(jt.isDone()) System.out.println(jt.getData());
        }
    }
    
    ...  //Whatever else.

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

Posted: Wed May 31, 2017 1:00 am
by admin
Because of the statement data += data; in run() method. data += data; is mathematically same as data = 2*data.

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

Posted: Wed Jul 04, 2018 5:31 pm
by zukras
Hi,

I don't understand when it might print 0, 1, 2.
  1. It will print something only if jt.isDone() returns true.
  2. jt.isDone() returns true only when run() is executed.
  3. When run() is executed data is always equals to 2*data
What I am missing in this question?

Thanks

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

Posted: Wed Jul 04, 2018 7:22 pm
by admin
You are not missing anything. It can't print 0, 1, 2. The question does not say that it may print 0, 1, 2 either.

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

Posted: Sat Jul 07, 2018 11:32 am
by prashantjain25
This got me confused it should also print 0,1,2 but it never does that. it prints all of these
0
0,2
0,4
0,2,4
but never 0,1,2 why?

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

Posted: Sat Jul 07, 2018 9:04 pm
by admin
You will need to read the explanation. It explains exactly why 0 1 2 cannot be printed.

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

Posted: Tue Aug 04, 2020 2:41 am
by moostebring
One question though. Hypothetically, if the first thread in the list for whatever reason is not ready upon the evaluation of the if(isDone()) method, it get's skipped and 2 - 4 would get printed right? Is that also the reason why the answers state 0 2 4 is a "possible" solution?

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

Posted: Tue Aug 04, 2020 5:13 am
by admin
That's right. But that is not the reason for 0 2 4 .
0 2 4 will be printed if all three threads are done before the main thread is able to execute the second loop.

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

Posted: Sat Mar 20, 2021 6:49 am
by jme_chg
Just to confirm,
all possible outputs are:
0 2 4
0 2
0 4
0
2 4
2
4
?

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

Posted: Sat Mar 20, 2021 11:03 am
by admin
That is correct.