wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes ? What is the advantage of using Runnable interface instead of extending Thread class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "? What is the advantage of using Runnable interface instead of extending Thread class" Watch "? What is the advantage of using Runnable interface instead of extending Thread class" New topic
Author

? What is the advantage of using Runnable interface instead of extending Thread class

thomas davis
Ranch Hand

Joined: Feb 01, 2003
Posts: 207
1)How do we create a thread using Runnable interface? Whether I need to call run() method of Runnable interface? What is the advantage of using Runnable interface instead of extending Thread class?
2)If I created a thread using Runnable interface and creating an object of it and passing to Thread Object, How can I call start method on different object ?
ClassRunnable objRunnable = new ClassRunnable ();
Thread t = new Thread (objRunnable);
t.start (); How I know the above code used for a particular thread ?
run()
1)the thread can execute its own run() method
2)the thread can execute the run() method of some other object
If you want the thread to execute its own run() method ,you need to subclass the Thread class and give your subclass a run()
Public class Counter extends Thread
{
public void run()
{
for(int i)
}
}
The run method just print 1.10 .To do this in a Thread, you first construct an instance of Counter and then invoke its start() method
1.Counter ct = new Counter();
2.ct.start() //start not run
What you do not do is call run() directly, instead you call start()The start() registers the thread (that is,ct) with thread scheduler.
If you want your thread to execute the run() method of some other object than itself, you still need to construct an instance of the Thread class. The only difference is that when you call the Thread constructor, you have to specify which object owns the run() method that you want
Public class Counter implements Runnable
{
public void run()
{
for(int i)
}
}
This class does not extend the Thread class, However, it has a run() method and it declares that it implements the Runnable interface. Thus any instance of the Counter class is eligible to be passed .
1.Counter c = new Counter();
2.Thread t = new Thread();
3.t.start();
Why Runnable rather than extending thread class ?
The run() method ,like any other member method ,is allowed to access the private data, and call private method, of the class of which it is a member. Putting run() in a subclass of Thread may mean that the method cannot get to features it needs .
Could somone throw light on the above statemnt?
2) Thread is the single implementation
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
One reason you'd want to implement an interface rather than extend a base class is that you are already extending some other class. You can only extend one class, but you can implement any number of interfaces.
Implementing Runnable means nothing more than you promise to have a run() method. The compiler checks to make sure you do. In your little example:

your ClassRunnable implements Runnable and has a run() method. When you create the new Thread t, you pass a reference to your objRunnable. When you call start() on t, t starts a new thread and calls your run() method on objRunnable. So now the code in your run() method is running on a new thread.
Hope that helps!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
From a design standpoint, it was an odd choice to have Thread implement Runnable and allow people to subclass Thread. I always implement Runnable both to avoid namespace clutter/encourage information hiding and because eventually today's single thread may be something else tomorrow (synchronous call, thread pool, etc.).
thomas davis
Ranch Hand

Joined: Feb 01, 2003
Posts: 207
I always implement Runnable both to avoid namespace clutter/encourage information hiding and because eventually today's single thread may be something else tomorrow

Could you please explain it ellaborately ..?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Thomas, having your class implement Runnable instead of extending Thread gives you the benefit of decoupling the piece of logic to execute and the workhorse -- the actual thread -- executing that logic.
If you extend Thread, you're basically preventing your logic to be executed by any other thread than 'this'. If you only want *some* thread to execute your logic, it's better to just implement Runnable.
Also, in Java extending Thread prevents you from extending any other class.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ? What is the advantage of using Runnable interface instead of extending Thread class
 
Similar Threads
When the execution ends?
Clarification wanted: Extending Threads vs. Implementing Runnable
consider code on threads
Interfaces over abstract classes
threads