Page 1 of 1
Re: About Question enthuware.ocpjp.v7.2.1330 :
Posted: Mon May 26, 2014 11:34 am
by kashyapa
Why couldn't it happen like this.
main thread calling t1.join()
Then main thread wait until t1 dies. hence it print "tom"
After t1 dies, main thread calling t2.join()
Hence it print "dick"
Ultimately it prints "harry".
Please explain me.
Re: About Question enthuware.ocpjp.v7.2.1330 :
Posted: Mon May 26, 2014 11:38 am
by admin
It could happen like that.
HTH,
Paul.
Re: About Question enthuware.ocpjp.v7.2.1330 :
Posted: Mon May 26, 2014 1:04 pm
by kashyapa
Not could, It should like that. isn't it?? because that is how compiler compile this.(statement by statement).
Re: About Question enthuware.ocpjp.v7.2.1330 :
Posted: Mon May 26, 2014 7:49 pm
by admin
A thread doesn't have to wait until some other thread issues a call to join on it.
Before the call to joins, there is: t1.start();t2.start();, which means, both the threads t1 and t2 are started already. It is therefore possible for main thread and t1 thread to be not running and t2 to be running thereby causing "dick" to be printed first.
Re: About Question enthuware.ocpjp.v7.2.1330 :
Posted: Sun Jun 12, 2016 12:11 pm
by dieterdde
Dears,
observation about this question. Is it not true that either of the join() calls might get interrupted, thus the main method would then throw the InterruptedException (because there's no try/catch), hence "harry" would not be printed at all, in that case?
If the above scenario is possible, it would then not be correct to say that "harry" will always print at the end.
Let me know please as I'm preparing for the OCP7
Cheers
Re: About Question enthuware.ocpjp.v7.2.1330 :
Posted: Sun Jun 12, 2016 9:05 pm
by admin
In the given code, there is no reason for the call to join to get interrupted.
Re: About Question enthuware.ocpjp.v7.2.1330 :
Posted: Mon Jun 13, 2016 2:50 am
by dieterdde
admin wrote:In the given code, there is no reason for the call to join to get interrupted.
Thanks, that means there's still a gap in my knowledge, as I don't understand when a thread exactly can get interrupted.
In the mean time I did some searching on the internet, and see that it can get caused when some other thread calls .interrupt() for example, like with Thread.currentThread().interrupt(), or the ExecutorService.shutdownNow() calls it as well.
This is what I LOVE about the enthuware exams, they always ask atleast a couple of things that weren't immediately clear from the book that I'm studying, K&B OCA/OCP7, as they don't go into any detail about .interrupt(), and I just scanned the book, they mention it one time briefly at the end of the next chapter of concurrency, but not in the current chapter of Threading that I just completed.
Cheers!