aspose file tools*
The moose likes Threads and Synchronization and the fly likes Thread/ Runnable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread/ Runnable" Watch "Thread/ Runnable" New topic
Author

Thread/ Runnable

aakash bhatt
Ranch Hand

Joined: Jan 09, 2003
Posts: 182
To incorporate threading we have Class Thread and interface runnable
So in which scenario we should use Class Thread or Runnable interface
Regards,
aakash
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Moving to Threads and Synchronization.
Gayathri Prasad
Ranch Hand

Joined: Jun 25, 2003
Posts: 116
Hi,
One Obvious reason is when U have multiple inheritance in place u cannot implement the Thread class thus we need to go for the interface.
Cheers,
Gaya3
Idly Vada
Ranch Hand

Joined: Sep 02, 2003
Posts: 135
When your thread class needs to extend some other class, you should go for Runnable interface.
Otherwise any approach is fine
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
IMO, it's better to implement the Runnable interface instead of extending Thread.


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
srinivasarao kamani
Greenhorn

Joined: Sep 18, 2003
Posts: 2
Better approach is
implement the "Runnable" interface.
and then pass this as a target to the
one of the Thread constructors.
[ September 19, 2003: Message edited by: srinivasarao kamani ]

kamani srinivasarao,<br />HCL Technologies Limited<br />Chennai-India.
Idly Vada
Ranch Hand

Joined: Sep 02, 2003
Posts: 135
Originally posted by srinivasarao kamani:
Better approach is
implement the "Runnable" interface.
and then pass this as a target to the
one of the Thread constructors.

Hi srinivasarao kamani!
When we use Runnable interface, this approach is the only approach to follow. Is there any other approach you are aware of?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch, srinivasarao.
___________________________________________________________________________
By using an object implementing Runnable we have decoupled the machinery to perform a task (the thread object) from the task itself (the Runnable object).
Now we can store or transmit the tasks to be carried out by a thread.
In theory there is also the following risk for an object extending Thread:
synchronized instance methods in such object might also to have to contend for the lock against the instance methods that were declared synchronized in the Thread class!
[ September 22, 2003: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
Jorge Phillips
Ranch Hand

Joined: Jun 03, 2001
Posts: 43
Inheritance semantically is analogous to "a kind of" relationship. There are classes where you want thread functionality but where the inheritance relationship doesn't make much sense. It is better in those cases to implement Runnable. Also if your class needs to extend another class, and yet access thread functionality, the only way to achieve this (since Java has no multiple inheritance) is to implement Runnable in addition to extending the parent class.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Any good examples where extending Thread is better? :roll:


Groovy
Kaustubh Patil
Ranch Hand

Joined: Aug 13, 2001
Posts: 164
Just adding my comments to already long discussion.
Weather to extend Thread or implemnt runnable depends on what exactly you are doing inside your run() method.
Lets take 2 cases :
1. You extend your class from the Thread class.
Here there is no interaction between the run method and the class members, because the run method from the parent class is used. i.e. if you want to access your class members inside the run method it cannot be done, unless you override the method in the parent Thread class.
2. You implement the runnable.
Here since you are writing the run() in your class itself. The run() method has full access to your class members. So there can be full interaction between the run method and the class.
-Kaustubh.


Kaustubh. Mumbai, India.
Idly Vada
Ranch Hand

Joined: Sep 02, 2003
Posts: 135
Originally posted by Kaustubh Patil:
Here there is no interaction between the run method and the class members, because the run method from the parent class is used. i.e. if you want to access your class members inside the run method it cannot be done, unless you override the method in the parent Thread class.

Why should anyone extend Thread class without overriding run() method in real world(except for theory purposes).
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
In general I prefer to implment Runnable rather than extend Thread. However there are two exceptions to this:
  • Cases where the code is very short. Instead of


  • I prefer to save a line:

    Or even just
  • Cases where I want to add some useful functionality to the Thread class, while still retaining the ability to use my new class for different Runnable implementations. E.g. I found that when I want to test a multi-threaded application, and an exception is thrown from a thread which I spawned for teting purposes, JUnit doesn't do a good job of catching and reporting this exception, since it's in a different thread than the one JUnit is monitoring. So I made a TestThread subclass which collects any exceptions thrown during run() and makes it easier to report them later. I suppose I could have made an abstract Runnable class which does the same thing, but the methods I was adding were related to thread control and monitoring more than they were to the actual Runnable task that was being implemented, so made logical sense to put my code in Thread subclass. And this way it can still be plugged in to any Runnable, for max flxibility. If I'd made an abstract class implementing Runnable, then any time I used it I couldn't inherit from any other class; I'd have to extend my TestRunnable class. Ugh. Putting my implementation in the Thread subclass kept the Runnable interface flexible.


  • "I'm not back." - Bill Harding, Twister
    Pradeep bhatt
    Ranch Hand

    Joined: Feb 27, 2002
    Posts: 8919

    Thanks Jim
    The first case rarely happens in real life.
    Ernest Friedman-Hill
    author and iconoclast
    Marshal

    Joined: Jul 08, 2003
    Posts: 24187
        
      34


    The first case rarely happens in real life

    Happens all the time in my code. Here the Thread is an anonymous inner class, which almost by definition is going to be fiddling with its outer class's stuff. If the body of run() is more than a line or three, it doesn't belong in an anonymous class, so I'll move it to a method in the outer class, and call it from run(). So single-line run() bodies are very common for me.


    [Jess in Action][AskingGoodQuestions]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Thread/ Runnable