About Question enthuware.ocpjp.v8.2.1078 :
Moderator: admin
-
- Posts: 4
- Joined: Sun Jul 31, 2016 12:21 pm
- Contact:
About Question enthuware.ocpjp.v8.2.1078 :
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
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
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1078 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Sun Jul 31, 2016 12:21 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1078 :
Ok, thanks for your help!
-
- Posts: 14
- Joined: Fri May 11, 2018 6:59 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1078 :
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
(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++);
}
}
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1078 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 14
- Joined: Fri May 11, 2018 6:59 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1078 :
Hello Paul, thank youadmin 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.
-
- Posts: 15
- Joined: Sun Jan 28, 2018 12:38 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1078 :
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 .
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 .
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1078 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 15
- Joined: Sun Jan 28, 2018 12:38 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1078 :
Yes you are correct I didn't notice that. Thanks for the quick reply
-
- Posts: 19
- Joined: Fri Aug 02, 2019 7:04 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1078 :
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?admin wrote: ↑Fri May 11, 2018 10:08 amThere 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.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1078 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 19
- Joined: Fri Aug 02, 2019 7:04 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1078 :
Thank you for the quick reply. Appreciate it.admin wrote: ↑Thu Dec 26, 2019 9:31 amcount++ 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.
Who is online
Users browsing this forum: Bing [Bot] and 101 guests