Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

Difference between two implementing the Thread

Thread view: 
Proton Projects - Moin - 24 Feb 2007 08:13 GMT
Hi all,

There are two way of Thread Implementation

1. Extending a Thread Class
2. Implementing the Runnable Interface

My Question are
1. What is the actual difference between the implementations?
2. Why Java provided the two way implementation

I got sick of hearing the regular answer saying that if we extend the
Thread Class...we cant extend any other class....I find this is a
silly justification...

I would like to know the root reason behind the two implementation

Kindly Answer
Moin
a249@mailinator.com - 24 Feb 2007 08:48 GMT
On 24 Feb., 09:13, "Proton Projects - Moin" <mohd.mohid...@gmail.com>
wrote:
> I got sick of hearing the regular answer saying that if we extend the
> Thread Class...we cant extend any other class....I find this is a
> silly justification...

So you are not prepared to listen to arguments which overwhelm your
pea size brain. Than go ahead and build your junk software the way you
prefer.
Proton Projects - Moin - 24 Feb 2007 09:04 GMT
On Feb 24, 1:48 pm, a...@mailinator.com wrote:
> On 24 Feb., 09:13, "Proton Projects - Moin" <mohd.mohid...@gmail.com>
> wrote:
[quoted text clipped - 6 lines]
> pea size brain. Than go ahead and build your junk software the way you
> prefer.

I dont want the answer who doesnt understand the situation of the
Questioner....I asked most of the people offline..but i got this
answer " if we extend the Thread Class...we cant extend any other
class...." i am not satisfied with the answer...for this reason...Java
has provided the functionality of two way implementation...

If you are about the answer the same reason....i dont need your
answer....

Regards
Moin
Daniel Dyer - 24 Feb 2007 13:54 GMT
> On Feb 24, 1:48 pm, a...@mailinator.com wrote:
>> On 24 Feb., 09:13, "Proton Projects - Moin" <mohd.mohid...@gmail.com>
[quoted text clipped - 13 lines]
> class...." i am not satisfied with the answer...for this reason...Java
> has provided the functionality of two way implementation...

The Runnable interface is effectively Java's substitute for function  
pointers.  All the Runnable interface says about a class is that here is  
some code that can be executed.  It doesn't say anything about where that  
code should be run or when.  You can run it on a new thread or you can run  
it on the current thread - it's up to you.

If, instead of implementing Runnable you extend Thread, you are tying the  
logic to a single particular concrete thread.  You can only start a thread  
once.  With a Runnable, you can pass it to several threads and run it as  
many times as you like, even simultaneously if it's written in a way that  
makes it safe to do so.

Another point that is often made when discussing this point is that by  
sub-classing Thread you are effectively saying this class is a new *type*  
of thread, when in reality it is not, it is just a normal thread that has  
an unnecessary coupling between what it does and how it does it.  So the  
reason not to sub-class thread is not so much because it prevents  
extending another class, but because conceptually it rarely makes sense  
for your Runnable to extend *any* base class.

Dan.

Signature

Daniel Dyer
https://watchmaker.dev.java.net - Evolutionary Algorithm Framework for Java

Eric Sosman - 24 Feb 2007 14:25 GMT
> Hi all,
>
> There are two way of Thread Implementation
>
> 1. Extending a Thread Class
> 2. Implementing the Runnable Interface

    The second is not a "way of Thread Implementation" because
it does not implement a Thread.  You can write as many classes
implementing Runnable as you please, and still have a program
that is only single-threaded (overtly, at least).  To launch
additional threads, you must create Thread objects and call
their start() methods.

> My Question are
> 1. What is the actual difference between the implementations?

    A Thread runs in its own thread of execution.  Runnable
is just an interface, and does nothing special at all.

> 2. Why Java provided the two way implementation

    Suppose it provided Runnable but not Thread.  Then there
would be no way to create a multi-threaded program.

    Suppose it provided Thread but not Runnable.  Then the
only way to get "your code" to run in a separate thread would
be for you to extend the Thread class.  This leads to two
further strategies: You can put "your code" directly in your
SubThread class, or you can put it in your Thing class and
have SubThread call a Thing method.  The first approach limits
your ability to build class hierarchies, and the second amounts
to reinventing Runnable for yourself.  Instead of having every
Java programmer in the world reinventing his own Runnable, Java
provides one ready-made.  You are not forced to use it.

> I got sick of hearing the regular answer saying that if we extend the
> Thread Class...we cant extend any other class....I find this is a
> silly justification...

    I'm sorry you are sick.  Some people find nausea entertaining,
but to the nauseous it is not the least bit funny.  Please stand
downwind until you feel better.

> I would like to know the root reason behind the two implementation

    The root cause is that the early Universe contained slightly
different quantities of matter and antimatter.  Had that not been
the case, Java would not be as it is.

Signature

Eric Sosman
esosman@acm-dot-org.invalid

Daniel Pitts - 24 Feb 2007 18:19 GMT
On Feb 24, 12:13 am, "Proton Projects - Moin"
<mohd.mohid...@gmail.com> wrote:
> Hi all,
>
[quoted text clipped - 15 lines]
> Kindly Answer
> Moin

Here is the difference:

creating a Thread object, and calling .start() on it will cause a new
thread of execution to start (you know this already...)

Thread.run() is the *always* what is run in the new thread.  The
implementation of Thread.run() is to call run() on the Runnable object
passed to it in the constructor.

When yo extend Thread, you override the run() method, so the default
behavior doesn't happen, but your behavior does.

HTH,
Daniel.
Chris Uppal - 24 Feb 2007 20:30 GMT
> 2. Why Java provided the two way implementation

Why does Java allow you to specify what a thread does using the Runnable
interface ?  You've already been told several times.

Why does Java /also/ allow you to do it by extending class Thread ?  Because
there is no reason to forbid it, and because for some (fairly rare)
applications it is desirable to attach extra information or behaviour to the
thread that executes some action.

   -- chris
Mike Schilling - 25 Feb 2007 05:39 GMT
> Hi all,
>
[quoted text clipped - 6 lines]
> 1. What is the actual difference between the implementations?
> 2. Why Java provided the two way implementation

In a sense, you're asking the question "Why can I extend the functionality
of a class either by subclassing or by aggregation and delegation?"  I don't
know an answer better than "Because Java supports both."


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.