About Question enthuware.ocpjp.v7.2.1330 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

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 »

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: 10045
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

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 »

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

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

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

Post 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.
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 »

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: 10045
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

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 »

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 22 guests