Author Topic:   start(),run()
Mani
unregistered
posted April 19, 2000 09:45 AM           
A silly question.

For creating a Thread we can either use Thread class or Runnable interface.

If we are extending Thread class in say aclass
we will override the run method in the Thread class.
Then we make the Thread runnable by calling start().
We are not passing any arguments (current class instace).
Neither we overriding start()
Then how will start() get to know which instance method to call??
If I'm not wrong start() in Thread class does nothing.
Is there any internal implementation to call run() whenever there is a call to start()???

Please correct me if my idea is wrong.

satya5
ranch hand
posted April 19, 2000 11:08 AM             

JLS specifies start like this:

"
20.20.14 public void start()
throws IllegalThreadStateException

Invoking this method causes this thread to begin execution;
this thread calls the run method of this Thread object.
The result is that two threads are running concurrently: the
current thread (which returns from the call to the start
method) and the thread represented by this Thread object
(which executes its run method).
"

(http://java.sun.com/docs/books/jls/html/javalang.doc18.html#8093)

Hope it helps ....

Regds.

- satya

maha anna
bartender
posted April 19, 2000 11:50 AM             
Mani,
Exactly. What you think is correct. The public void start() does exactly the same task what you suspected. It is NOT do-nothing method. It calls the run method of the object with which the start() was made. In order to show this in action I wrote a small appln. for you. Try this. The start() method in MyThread class toggles between do-nothing start() and as regular start() when you override and not-override the start() method.
regds
maha anna

class MyThread extends Thread {

public void run() {
for(int i=0; i<10; i++) {
System.out.println("i="+i);
}
}

//Try to override start()

public void start() {
System.out.println("See. It's me . Do-nothing start() ");
}

public static void main(String[] args) {

MyThread myT = new MyThread();
myT.start();

}
}


[This message has been edited by maha anna (edited April 19, 2000).]

Ajith Kallambella
ranch hand
posted April 19, 2000 12:21 PM             
One word reply. Polymorphism( run-time binding ).

Does this ring any bells??

Mani
unregistered
posted April 19, 2000 10:47 PM           
Let me explain what I understood from this .
Correct me if it is wrong.

1) When we use Thread class for creating Threads and when we call the current class object.start() , if we do not override the start method in the current class , Thread class start() will be executed and it will call current class's run() if it is present.

2)When we use Runnable for creating Thread we create a Thread object and pass current object reference as constructor argument.
After this if we call Thread object's start() , it will call Thread class start() which calls run in the current class if it is present.


maha anna
bartender
posted April 20, 2000 12:18 AM             
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


class MyThread extends Thread{
public void run() {.... }
public static void main(String[] args) {
MyThread myT = new MyThread();
myT.start();
}
}


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



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() {....}
}


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.



class Test {
public static void main(String[] args) {
Runnable rc = new RunnableClass();
Thread myT = new Thread();//Do nothing thread
myT.start(); //See here
}
}

regds
maha anna

[This message has been edited by maha anna (edited April 20, 2000).]

Mani
unregistered
posted April 20, 2000 12:41 AM           
Thanks Maha.Now the concept is almost clear.Need more reading.

|