2/18/2006

Why Thread implements Runnable?

I have this questions for many years, why Thread implemets Runnable?

We implement Runnable, if we want our method (task) to be called from a new thread or a special thread. Then we do some thing like below.

public void doTaskInRunnable() {
Runnable r = new Runnable() {
public void run() {
task();
}
};

// to make it tarket of a new thread.
Thread thread = new Thread(r);
thread.start();
// or to make our task run from a sp thread.
SwingUtilities.invokeLater(r);
}

If we use Thread instead of Runnable above code becomes

public void doTaskInRunnable() {
Thread r = new Thread() {
public void run() {
task();
}
};
// Reason 1 : to make it tarket of a new thread.
Thread thread = new Thread(r);
thread.start();
// or Reason 2 : to make our task run from a sp thread.
SwingUtilities.invokeLater(r);
}

Making a Thread target of another new Thread is plain stupid, why create a new thread and call it from another at all, in the above code at Reason1 we can as well call r.start().
Making a Thread target of a special thread is confusing, why a thread is running in another thread.
Until java 5, if we use Thread in place of Runnable, we have a memory leak. In the code above thread r is never started--it's run is called from another thread but never started, until java 5 ThreadGroup used to keep track of all un-started threads and r is never released.

Using Thread in place of Runnable is ugly, then why Thread implements Runnable? Is it because they have same method named run with same signature?

4 Comments:

At 2/19/2006, Blogger Avah said...

I think it's so you could extend it and override the "run" method, instead of creating an instance of Runnable and pass it to the Thread.

 
At 2/19/2006, Anonymous Anonymous said...

By implementing runnable, though little confusing, a thread can run muliple times.

 
At 2/19/2006, Blogger Bob said...

Less code.

 
At 8/11/2007, Blogger Srini said...

My 2 cents. Runnable is an interface and is not a thread. Defending why Java needs that mechanism, being an ardent lover of Java,

1) If we are developing a framework and we want users to provide tasks that can be executed in parallel, we can enforce them to implement run method by asking them to implement Runnable interface, and threads enforce that contract via run method and start method. This enables us as framework developers to accept runnable objects. Otherwise we have to accept (Object-> error prone as it is too generic or Thread --> which is not client wants to do either due to Multiple Inheritance limitation or because he doesn't care how this object gets executed.

2) If we are using a library, that has computations like sort, then if the library can implement Runnable interface, our job becomes easy, as we just have to call a thread constructor like given in your code and invoke it using start. Otherwise, we will have to extend from a new thread and may have to create a new class.

To answer "Why Thread implements Runnable", without bothering about clients classes a framework developer can execute client tasks (either extends Thread or implements Runnable) uniformly. Without that he has to use instanceof.

 

Post a Comment

<< Home