• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Diff between Thread and Runnable

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Is there any specifi difference between using Thread class and Runnable interface in Java Multithreading.

regards
Sree Nivas
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every Thread has a Runnable -- sometimes the Runnable is the Thread itself. In any case, it's the Runnable's job to provide a run() method, and it's the Thread's job to provide the magic machinery to execute in a separate thread.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can search this forum for that question. Like search for Runnable. This topic has been covered exhaustively here.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
You are asking the differences between the Thread class and the Runnable interface. Both are used to make threads. Implimenting the Runnable interface is appropiate than using the Thread class. Because we can extend another class while making a thread. But if we just extend the Thread class we can't inherit from another class. There is another difference. We can create threads easily using the Thread class than using the Runnable interface. Because we can just make an object from our class.But when implementing the Runnable interface we have to do a lot making thread objects giving arguments and so on. For furthur explanations see the JLS
thanks
 
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajith has right, a thread is class and it implements the Runnable - a thread is a runnable and it is used by threads(direct or indirect).
The diference between the two is that one is a implementation(the Thread) and the other one (Runnable) is an interface.
In the oop "words" the Thread IS A Runnable(extend) because it implements the Runnable interface, can be also Thread HAS A Runnable(composition) - because on a thread you can set the threads target(via its contructor).
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Difference Between Threads class & Runnable Interface is::

java has 2 ways of creating the threads...
- implementing an interface
- Extending a class.

Extending the class is the way java inherits methods and variable from a parent class. In this case one can only extend or inherit from a single parent class.

This limitation within java can be overcome by implementing the interfaces, which is the most common way to create threads.

Interfaces provide a way for the programmers to lay the groundwork of a class. They are used to design the requirements for a set of classes to implement. The inteface sets everything up, and the class or classes that implement the interface do all the work. The different set of classes that implement the interface have to follow the same rules.

I think i've cleared ur doubt abt the Threads class & Runnable Interface.

Please Stop me if i'm going Wrong.

Thanxs
Sheetal
 
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

One difference is that you get more help in validating your code when you implement an interface. This example is a bit trivial:



It compiles fine. You might not realize that you miss-spelled run and wonder why it does not work. Probably more important when writing mouse listeners, etc. If you implemented the interface you would have got a compile time error!

In any case, the main aim of this exercise is to have the run entry point correctly available to the JRE. In this example it would have jumped into Thread's empty run method.

Ed
[ December 28, 2004: Message edited by: Eddie Vanda ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think about it from an 00 perspective. What's the purpose of subclassing? Remember that we're talking about two different things here-the Thread and the thread'slob. From an 00 view, those
two are very separate activities, and belong in separate classes. The only time you want to subclass/extend
the Thread class,Is if you are making a new and more specific type ofThread.ln other words, if you think of the Thread as the worker, don't extend the Thread class
unless you need more specific worker behaviors. But If all you need Is a new lob to be run by a Thread/worker, then Implement Runnable In a separate,job-specific
(not worker-specific) class. This is a design Issue and not a performance or language Issue.It's perfectly legal to subclass Thread and override the runO method, but It's rarely a good Idea.
 
Marshal
Posts: 80226
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic