About Question enthuware.ocpjp.v7.2.1387 :

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

Moderator: admin

Post Reply
The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

About Question enthuware.ocpjp.v7.2.1387 :

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

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

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

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

The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

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

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

The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

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

Post by The_Nick »

Ok forget about it, however the MyThread.run() method eventually get called as it gets called by Thread.run().

colmkav
Posts: 21
Joined: Thu Jul 16, 2015 4:22 am
Contact:

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

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

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

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

Post by admin »

There is no issue.The given code and answer is correct.

krohani
Posts: 31
Joined: Tue Oct 06, 2015 1:57 pm
Contact:

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

Post 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?

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

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

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

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

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

Post 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?

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

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

Post by admin »

You need to read the explanation carefully. It explains exactly what you are asking.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests