About Question enthuware.ocpjp.v7.2.1456 :

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

Moderator: admin

Post Reply
renatumb
Posts: 47
Joined: Mon Apr 08, 2013 7:55 pm
Contact:

About Question enthuware.ocpjp.v7.2.1456 :

Post by renatumb »

That following piece sounds strange:
Note that this method is not Abstract in the Thread Class. So you need not necessarily override it. But the Thread class version
doesn't do anything. It is just empty implementation.
Because:
1 - If your thread extends from Thread class, and if want that your class do some work, you must override the run method.

2 - Also the run's Thread Class is not a empty method, there is some code inside there that is really important to know.
So if you do like this:

Code: Select all

        new Thread( new X() ).run(); // It calls the run() of object that is inside Thread's constructor -                  .                                                           // same thread, so if you code something in X's run(), it will be executed !!
        new Thread( new X()).start(); // Correct way, X's run() runs but in another Thread

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

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

Post by admin »

The code in Thread's run method is:

Code: Select all

public void run() {
          if (target != null) {
               target.run();
          }
}
So you could say that it is not totally empty but, practically, it is not really doing anything because if you extend Thread, your target will probably be null.

So new MyThread().start(); will basically be doing nothing.

But you are right that the explanation should make that clear.

HTH,
Paul.

ewebxml
Posts: 77
Joined: Sun Jun 30, 2013 10:04 pm
Contact:

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

Post by ewebxml »

Currently says:

A call to start() returns immediately but before returning,
it internally causes a call to the run method of either the Thread class (if the thread was created by doing new ClassThatExtendsThread()) or of the Runnable class (if the thread was created by doing  new Thread( classImplementingRunnable);)
___________________________________________

Question: For the last sentence, should the word class be changed to interface?
-----
A call to start() returns immediately but before returning,
it internally causes a call to the run method of either the Thread class (if the thread was created by doing new ClassThatExtendsThread()) or of the Runnable interface (if the thread was created by doing  new Thread( classImplementingRunnable);)

-----

Please confirm.

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

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

Post by admin »

Not really. Runnable is indeed an interface but what it is trying to say by "Runnable class" is "a class that is-a Runnable" i.e. a class that implements Runnable. Similarly, by "Thread class", it means a class that is-a Thread i.e. a class that extends Thread.
However, to avoid the confusion, I am changing it to instance.
HTH,
Paul.

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests