This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

start(),run()

 
Mani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
<pre>

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();
}
}

</pre>

[This message has been edited by maha anna (edited April 19, 2000).]
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One word reply. Polymorphism( run-time binding ).
Does this ring any bells??
 
Mani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Mani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Maha.Now the concept is almost clear.Need more reading.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic