Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Diff between Thread and Runnable

 
Sree Nivas
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

regards
Sree Nivas
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can search this forum for that question. Like search for Runnable. This topic has been covered exhaustively here.
 
Rajith Vidanaarachchi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • 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).
 
Sheetal Kaul
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • 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
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic