About Question enthuware.ocpjp.v7.2.1729 :
Moderator: admin
-
- Posts: 1
- Joined: Fri Nov 14, 2014 7:38 pm
About Question enthuware.ocpjp.v7.2.1729 :
Since the code in main method has t1.join() and t2.join() , the main thread will wait till t1 and t2 ends and then executes System.out.println(counter);
so , it will always print 0.
Why will it print any number between -5000 and 5000 ?
so , it will always print 0.
Why will it print any number between -5000 and 5000 ?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1729 :
Did you read the explanation? It explains exactly why it may print any number between -5000 and 5000.
If you like our products and services, please help us by posting your review here.
-
- Posts: 62
- Joined: Fri Aug 07, 2015 2:16 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1729 :
But it could happen that t2 finishes before t2 starts, so t1 will spin forever.
or will it never see the update on the counter because counter is not volotile?
or will it never see the update on the counter because counter is not volotile?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1729 :
I not sure what you mean by t1 will spin for ever. It will only execute the loop as long as i is less that 5000 and it does increment i after every iteration. So why do you think it will keep running?sir_Anduin@yahoo.de wrote:But it could happen that t2 finishes before t2 starts, so t1 will spin forever.
It is possible that it will never see counter updated. But that has nothing to do with loop iterations. Loop variable is i and not counter.or will it never see the update on the counter because counter is not volotile?
If you like our products and services, please help us by posting your review here.
-
- Posts: 62
- Joined: Fri Aug 07, 2015 2:16 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1729 :
got it, thanks
-
- Posts: 3
- Joined: Wed Apr 05, 2017 1:33 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1729 :
I don't get this. I ran the code 100 times and it always print 0, so the answer should be "It will always print 0"?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1729 :
Did you read the explanation? You may see it print 0 a million times but there is no guarantee that it will print 0 one million one-th time.
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Wed Apr 05, 2017 1:33 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1729 :
admin wrote:Did you read the explanation? You may see it print 0 a million times but there is no guarantee that it will print 0 one million one-th time.
okay great. thanks. that makes sense
Who is online
Users browsing this forum: Bing [Bot] and 49 guests