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.
enthuware.ocpjp.v7.2.1336
Moderator: admin
-
- Posts: 28
- Joined: Wed Feb 25, 2015 9:03 am
- Contact:
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: enthuware.ocpjp.v7.2.1336
Given answer is correct. Kindly read the explanation.
If you like our products and services, please help us by posting your review here.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: enthuware.ocpjp.v7.2.1336
Code: Select all
"Thread.currentThread().setName("Second")"
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: enthuware.ocpjp.v7.2.1336
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.
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.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: enthuware.ocpjp.v7.2.1336
admin wrote: ↑Sun Mar 17, 2019 9:29 pmThread.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.
This is from the solution of this question. I am kind of confused.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.
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: enthuware.ocpjp.v7.2.1336
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").
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.
Who is online
Users browsing this forum: No registered users and 40 guests