Page 1 of 1

About Question enthuware.ocpjp.v7.2.1387 :

Posted: Sun Jun 16, 2013 10:53 am
by The_Nick
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.

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

Posted: Mon Jun 17, 2013 12:05 pm
by admin
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.

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

Posted: Tue Sep 03, 2013 4:54 am
by The_Nick
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.

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

Posted: Tue Sep 03, 2013 5:25 am
by The_Nick
Ok forget about it, however the MyThread.run() method eventually get called as it gets called by Thread.run().

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

Posted: Tue Aug 18, 2015 11:58 am
by colmkav
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.

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

Posted: Tue Aug 18, 2015 8:43 pm
by admin
There is no issue.The given code and answer is correct.

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

Posted: Sun Oct 11, 2015 2:56 pm
by krohani
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?

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

Posted: Sun Oct 11, 2015 9:39 pm
by admin
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.

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

Posted: Wed Feb 20, 2019 3:33 pm
by crazymind
Why does it print in order? It treats these two tasks consume same execution time; therefore, who starts firstly also ends firstly as well?

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

Posted: Thu Feb 21, 2019 9:27 am
by admin
You need to read the explanation carefully. It explains exactly what you are asking.