About Question enthuware.ocpjp.v7.2.1330 :

Moderator: admin

Post Reply
kashyapa
Posts: 23
Joined: Thu May 08, 2014 5:27 am
Contact:

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

Post by kashyapa » Mon May 26, 2014 11:34 am

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.

admin
Site Admin
Posts: 8702
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Mon May 26, 2014 11:38 am

It could happen like that.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

kashyapa
Posts: 23
Joined: Thu May 08, 2014 5:27 am
Contact:

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

Post by kashyapa » Mon May 26, 2014 1:04 pm

Not could, It should like that. isn't it?? because that is how compiler compile this.(statement by statement).

admin
Site Admin
Posts: 8702
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Mon May 26, 2014 7:49 pm

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.
If you like our products and services, please help us by posting your review here.

dieterdde
Posts: 19
Joined: Wed May 25, 2016 4:33 am
Contact:

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

Post by dieterdde » Sun Jun 12, 2016 12:11 pm

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

admin
Site Admin
Posts: 8702
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sun Jun 12, 2016 9:05 pm

In the given code, there is no reason for the call to join to get interrupted.
If you like our products and services, please help us by posting your review here.

dieterdde
Posts: 19
Joined: Wed May 25, 2016 4:33 am
Contact:

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

Post by dieterdde » Mon Jun 13, 2016 2:50 am

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!

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests