Difference between two implementing the Thread

  • Thread starter Proton Projects - Moin
  • Start date
P

Proton Projects - Moin

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
 
A

a249

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

Proton Projects - Moin

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.

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
 
D

Daniel Dyer

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

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

Eric Sosman

Proton said:
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.
 
D

Daniel Pitts

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

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

Chris Uppal

Proton said:
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
 
M

Mike Schilling

Proton said:
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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top