About Question enthuware.ocpjp.v7.2.1387 :
Moderator: admin
-
- Posts: 132
- Joined: Thu May 16, 2013 9:23 am
- Contact:
About Question enthuware.ocpjp.v7.2.1387 :
Why MyThread.run does not get called? I understand that everything happens on the "main" thread. However to print that output the above implemented run method should get called. So class MyThread not Thread.
What am I missing here?
Thanks in advance.
The_Nick.
What am I missing here?
Thanks in advance.
The_Nick.
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1387 :
Did you read the explanation?
It is not called because you are noting invoking start() method on the Thread objects. You are directly calling the run() method of the Thread objects (not of the MyThread object). The start() method of Thread class calls run() method of the Runnable. So if you had called start() instead of run() on the Thread objects, the run method of MyThread object would have been called.
It is not called because you are noting invoking start() method on the Thread objects. You are directly calling the run() method of the Thread objects (not of the MyThread object). The start() method of Thread class calls run() method of the Runnable. So if you had called start() instead of run() on the Thread objects, the run method of MyThread object would have been called.
-
- Posts: 132
- Joined: Thu May 16, 2013 9:23 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1387 :
The last time as this post confirm, the right answer was prints only end because the run method of MyThread does not get called? What has it changed?
Thanks in advance.
The_Nick.
Thanks in advance.
The_Nick.
-
- Posts: 132
- Joined: Thu May 16, 2013 9:23 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1387 :
Ok forget about it, however the MyThread.run() method eventually get called as it gets called by Thread.run().
-
- Posts: 21
- Joined: Thu Jul 16, 2015 4:22 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1387 :
Can we please have some clarification on the above? I went for option 2 but I now see there could be an issue as to what run() method is actually called.
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1387 :
There is no issue.The given code and answer is correct.
-
- Posts: 31
- Joined: Tue Oct 06, 2015 1:57 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1387 :
admin wrote:Did you read the explanation?
It is not called because you are not invoking start() method on the Thread objects. You are directly calling the run() method of the Thread objects (not of the MyThread object). The start() method of Thread class calls run() method of the Runnable. So if you had called start() instead of run() on the Thread objects, the run method of MyThread object would have been called.
There needs to be some clarification here because of the explanation provided by the site admin above. The site admin says that "...if you had called start() instead of run() on the Thread objects, the run method of MyThread object would have been called." which means that since we did NOT class start then the run method of MyThread should not have been called. But the explanation to the answer states otherwise...
Can someone please clarify?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1387 :
When you call start() on a Thread (or its subclass) object, it internally invokes the run() method of that class (or the run() method of the Runnable instance, if the Thread is created using a Runnable instance) in a separate thread.
When you call run() on a Thread class, it directly invokes the run() method of that class (or the run() method of the Runnable instance, if the Thread is created using a Runnable instance) in the same thread.
HTH,
Paul.
When you call run() on a Thread class, it directly invokes the run() method of that class (or the run() method of the Runnable instance, if the Thread is created using a Runnable instance) in the same thread.
HTH,
Paul.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1387 :
Why does it print in order? It treats these two tasks consume same execution time; therefore, who starts firstly also ends firstly as well?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1387 :
You need to read the explanation carefully. It explains exactly what you are asking.
Who is online
Users browsing this forum: No registered users and 10 guests