aspose file tools*
The moose likes Threads and Synchronization and the fly likes Diff between Thread and Runnable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Diff between Thread and Runnable" Watch "Diff between Thread and Runnable" New topic
Author

Diff between Thread and Runnable

Sree Nivas
Ranch Hand

Joined: Jul 24, 2003
Posts: 95
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

You can search this forum for that question. Like search for Runnable. This topic has been covered exhaustively here.
Rajith Vidanaarachchi
Greenhorn

Joined: Nov 06, 2004
Posts: 11
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

Joined: Sep 18, 2003
Posts: 916

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).


SCJP, SCJD, SCWCD, OCPJBCD
Sheetal Kaul
Ranch Hand

Joined: Nov 29, 2004
Posts: 47
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

Joined: Mar 18, 2003
Posts: 281
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 ]

The nice thing about Standards is that there are so many to choose from!
 
wood burning stoves
 
subject: Diff between Thread and Runnable
 
Similar Threads
Thread class
thread
Thread vs Runnable
Runnable to Thread Casting
Clarification wanted: Extending Threads vs. Implementing Runnable