Query:difference between different mothod to create a thread!

J

Jack Dowson

Hello Everybody:
As we all know there are two ways to create a thread,one is by
inheriting class Thread another is by implementing interface Runnable.
An instance created by class Thread can not be reused while the one
created by interface Runnable could.Why?I think it has nothing to do
with java's single inherit or multi interface implement.If it really
does,how does this rule work then?

Any help will be greatly appreciated!
Dowson!
 
K

Knute Johnson

Jack said:
Hello Everybody:
As we all know there are two ways to create a thread,one is by
inheriting class Thread another is by implementing interface Runnable.
An instance created by class Thread can not be reused while the one
created by interface Runnable could.Why?I think it has nothing to do
with java's single inherit or multi interface implement.If it really
does,how does this rule work then?

Any help will be greatly appreciated!
Dowson!

From the docs

public void start()

Causes this thread to begin execution; the Java Virtual Machine
calls the run method of this thread.

The result is that two threads are running concurrently: the current
thread (which returns from the call to the start method) and the other
thread (which executes its run method).

It is never legal to start a thread more than once. In particular, a
thread may not be restarted once it has completed execution.

Throws:
IllegalThreadStateException - if the thread was already started.
See Also:
run(), stop()
 
A

a24900

As we all know there are two ways to create a thread,one is by
inheriting class Thread another is by implementing interface Runnable.

Subclassing Thread: bad

Implementing Runnable and creating a separate Thread to start it: good
An instance created by class Thread can not be reused while the one
created by interface Runnable could.Why?

The underlying operating-system, platform-specific thread used by the
Thread object is dead after one usage and can't be resurrected.

For the rest of your question: Rework them to make sense.
 
E

Esmond Pitt

Jack said:
As we all know there are two ways to create a thread,one is by
inheriting class Thread another is by implementing interface Runnable.

There is only one way to create a Thread, and that's by calling one of
the overloads of new Thread(...). There are two ways of *providing the
Runnable code that the thread will execute*: extending Thread, or
implementing a Runnable and supplying it to the Thread's constructor.
Orthogonality concerns suggest that the latter is generally the
preferable approach.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top