Page 1 of 1

About Question enthuware.ocpjp.v7.2.1456 :

Posted: Wed Jul 24, 2013 1:35 pm
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

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

Posted: Wed Jul 24, 2013 3:34 pm
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.

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

Posted: Fri Jul 10, 2015 10:42 am
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.

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

Posted: Fri Jul 10, 2015 10:13 pm
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.