enthuware.ocpjp.v7.2.1336

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

Moderator: admin

Post Reply
ThufirHawat
Posts: 28
Joined: Wed Feb 25, 2015 9:03 am
Contact:

Re: enthuware.ocpjp.v7.2.1336

Post by ThufirHawat »

The right answer "Second will never be printed before First".
I'm sure is wrong, I ran 5 times this code, and the order is random.

The answer should be
"It may print First, Second and MyRunnable in any order."

Please confirm.

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

Re: enthuware.ocpjp.v7.2.1336

Post by admin »

Given answer is correct. Kindly read the explanation.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: enthuware.ocpjp.v7.2.1336

Post by crazymind »

Code: Select all

"Thread.currentThread().setName("Second")"
Hi, Which thread's name was set to Second in this code? Is main thread (First) or MyRunnable? Thanks.

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

Re: enthuware.ocpjp.v7.2.1336

Post by admin »

Thread.currentThread() gives you the reference to whichever thread executes this statement. So, in this case, it is the main thread.
Also, there is no MyRunnable thread. In fact, there is only one thread, the main thread, in this program. No other thread is created.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: enthuware.ocpjp.v7.2.1336

Post by crazymind »

admin wrote:
Sun Mar 17, 2019 9:29 pm
Thread.currentThread() gives you the reference to whichever thread executes this statement. So, in this case, it is the main thread.
Also, there is no MyRunnable thread. In fact, there is only one thread, the main thread, in this program. No other thread is created.
Now there are two threads running (or ready to run state): The main thread (having the name "First") and the MyRunnable thread (having the name "MyRunnable"). Any of these two threads may be allowed to run by the OS.
This is from the solution of this question. I am kind of confused.

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

Re: enthuware.ocpjp.v7.2.1336

Post by admin »

Sorry, I saw a different question that was similar. The explanation given in the question is correct.
But the answer that I gave to your query is still the same - Thread.currentThread() gives you the reference to whichever thread executes this statement. So, in this case, it is the main thread who name is being by the statement Thread.currentThread().setName("Second").
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 40 guests