About Question enthuware.ocpjp.v8.2.1391 :
Moderator: admin
-
- Posts: 2
- Joined: Thu Nov 24, 2016 7:26 am
- Contact:
About Question enthuware.ocpjp.v8.2.1391 :
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?
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1391 :
Did you read the explanation? It explains exactly what you are asking.
If you like our products and services, please help us by posting your review here.
-
- Posts: 12
- Joined: Mon Jan 30, 2017 2:58 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1391 :
Can you please explain why the results are always ordered?
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1391 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Fri Feb 03, 2017 3:25 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1391 :
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.
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.
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1391 :
Because of the statement data += data; in run() method. data += data; is mathematically same as data = 2*data.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Fri Jun 02, 2017 4:57 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1391 :
Hi,
I don't understand when it might print 0, 1, 2.
Thanks
I don't understand when it might print 0, 1, 2.
- It will print something only if jt.isDone() returns true.
- jt.isDone() returns true only when run() is executed.
- When run() is executed data is always equals to 2*data
Thanks
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1391 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 9
- Joined: Sat Feb 15, 2014 12:38 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1391 :
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?
0
0,2
0,4
0,2,4
but never 0,1,2 why?
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1391 :
You will need to read the explanation. It explains exactly why 0 1 2 cannot be printed.
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Tue Jul 21, 2020 3:28 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1391 :
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?
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1391 :
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.
0 2 4 will be printed if all three threads are done before the main thread is able to execute the second loop.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Sun Feb 07, 2021 6:30 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1391 :
Just to confirm,
all possible outputs are:
0 2 4
0 2
0 4
0
2 4
2
4
?
all possible outputs are:
0 2 4
0 2
0 4
0
2 4
2
4
?
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1391 :
That is correct.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 11 guests