• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

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

 
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ..?
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic