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 Please correct me if my idea is wrong.
|
satya5 ranch hand |
posted April 19, 2000 11:08 AM
JLS specifies start like this: " Invoking this method causes this thread to begin execution; (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
[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.
|
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
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
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
regds [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.
|
| | |