This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Why Thread.start()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Why Thread.start()?" Watch "Why Thread.start()?" New topic
Author

Why Thread.start()?

Arun Maalik
Ranch Hand

Joined: Oct 25, 2005
Posts: 216
Dear ranchar i having a code for a thread as below


Well the above code will work fine as i have expected but tell me why we are calling the start method by Thread object we can call the start method as below

First p=new First();
p.start();

and will pwork fine then what is the need of Thread object there.

Regard
Arun
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
You are right, it's not needed.

Or better yet, it's not necessary that First extends Thread. Let it implement Runnable instead, and all still is fine. (Your code works because Thread implements Runnable, too.)


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Compromise:

Thread first = new First();
first.start();

Bu. ;)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
We have a number of reasons to prefer implementing Runnable over extending Thread.

When you extend any concrete class you expose yourself to risk in the event the superclass changes one day. This article explains why in perhaps overly dramatic language.

When you extend Thread you tell the world that your task "is a" thread, which isn't true (unless you want to modify the way Threads work.) It's some code that can be run on a thread.

When you implement Runnable you can also extend something else. If you extend Thread you're stuck with Thread.

None of those are drop dead reasons to never extend Thread, but they add up to a strong preference to get in the habit of implementing Runnable.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why Thread.start()?
 
Similar Threads
Thread Ques
Thread
why StackOverflowError?
Thread Doubt
Runnable Issue