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 / May 2007

Tip: Looking for answers? Try searching our database.

Query:difference between different mothod to create a thread!

Thread view: 
Jack Dowson - 04 May 2007 19:13 GMT
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!
Knute Johnson - 04 May 2007 19:49 GMT
> Hello Everybody:
> As we all know there are two ways to create a thread,one is by
[quoted text clipped - 6 lines]
> 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()

Signature

Knute Johnson
email s/nospam/knute/

a24900@googlemail.com - 04 May 2007 19:51 GMT
> 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.
Esmond Pitt - 07 May 2007 08:10 GMT
> 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.


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.