File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes why we need to override run Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why we need to override run " Watch "why we need to override run " New topic
Author

why we need to override run

abhay jain
Ranch Hand

Joined: Jun 03, 2011
Posts: 130
why we need to override run ( ) to create a thread ?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

Because that's how they work! To create a thread you do one of two things:

- Write a class that extends Thread
- Write a class that implements Runnable, and pass an instance of that to a Thread object (via its constructor)

In the second case, you have to implement run() because it's part of the Runnable interface. In the first case, you don't have to override run(), but your thread won't do anything - without it you'd just get a new thread that exits immediately.

This is what is happening: when a thread is started it executes the run() method of the Thread object. The default implementation of this runs the run() method of any Runnable you've passed in. So that's where you have to put whatever code you want performing.
 
 
subject: why we need to override run
 
Similar Threads
hashcode
Can we define run() in another method of the class ??
Object hashCode ?
Thread question-no run() method signature
Primary Key Class