Mani,
It is like this. You make a class behave as a thread by means of 1.
extending the Thread class .
2. by making the class to implement the Runnable interface and create a thread using an instance of this class which implements the interface (in other words a Runnable object ref) to the argument of the Thread constructor.
3. Note that when you extend a Thread class , the subclass
is a thread by itself in nature. Because it inherits all the Thread related behaviour.
4. When you create a class just by implementing the Runnable interface, it is NOT YET a thread by nature. It has just the work to do. But not the worker
who actaully does the work.
5.In the first case
<pre>
class MyThread extends Thread{
public void run() {.... }
public static void main(String[] args) {
MyThread myT = new MyThread();
myT.start();
}
}
</pre>
In this case you are
OVERRIDING
public void run() { } and
invoking the inherited public void start() { } So as you said when you call theThereadObject.start(), What happens is the silent inherited start()
, calls the overridden run() in 'theThreadObject'.
For the 2nd case <pre>
class Test {
public static void main(String[] args) {
Runnable rc = new RunnableClass();
Thread myT = new Thread(rc);
myT.start(); //See here
}
}
class RunnableClass implements Runnable {
public void run() {....}
}
</pre>
For this case the same old silent start() in Thread class is called in '//See here' line,
but now the Thread object knows whose run() to call which now is the
run() defined in the Runnable object.
For the 3rd case Also note that there is one more constructor for Thread. Which is a plain do-nothing thread.like the foll. When you call myT.start() for this case, it calls the plain do-nothin run() method in the Thread class and nothing is happens due to this started thread.
<pre>
class Test {
public static void main(String[] args) {
Runnable rc = new RunnableClass();
Thread myT = new Thread();//Do nothing thread
myT.start(); //See here
}
}
</pre>
regds
maha anna
[This message has been edited by maha anna (edited April 20, 2000).]