Page 1 of 1

About Question enthuware.ocpjp.v7.2.1729 :

Posted: Fri Nov 14, 2014 7:53 pm
by kavitha.veluri
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 ?

Re: About Question enthuware.ocpjp.v7.2.1729 :

Posted: Sat Nov 15, 2014 12:15 am
by admin
Did you read the explanation? It explains exactly why it may print any number between -5000 and 5000.

Re: About Question enthuware.ocpjp.v7.2.1729 :

Posted: Fri Jan 20, 2017 2:47 pm
by sir_Anduin@yahoo.de
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?

Re: About Question enthuware.ocpjp.v7.2.1729 :

Posted: Fri Jan 20, 2017 10:57 pm
by admin
sir_Anduin@yahoo.de wrote:But it could happen that t2 finishes before t2 starts, so t1 will spin forever.
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?
or will it never see the update on the counter because counter is not volotile?
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.

Re: About Question enthuware.ocpjp.v7.2.1729 :

Posted: Sun Jan 22, 2017 4:17 am
by sir_Anduin@yahoo.de
got it, thanks

Re: About Question enthuware.ocpjp.v7.2.1729 :

Posted: Tue Apr 11, 2017 10:42 am
by javaace
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"?

Re: About Question enthuware.ocpjp.v7.2.1729 :

Posted: Tue Apr 11, 2017 12:24 pm
by admin
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.

Re: About Question enthuware.ocpjp.v7.2.1729 :

Posted: Wed Apr 12, 2017 7:31 am
by javaace
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