Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Overriding start() in Threads 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 » Beginning Java
Bookmark "Overriding start() in Threads" Watch "Overriding start() in Threads" New topic
Author

Overriding start() in Threads

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Overriding start() in Threads
When I override start method in a Thread created by „extends Thread
and a Thread created by „implements Runnable“ there is a different
reaction – WHY?

Result:
C:\>java BackgroundTest4a
In start()
 Thread is not started but start() method of Background object is invoked.

C:\>java BackgroundTest4
Thread-2
 Thread is started and not start() von Background is invoked!
Neil Laurance
Ranch Hand

Joined: Jul 18, 2002
Posts: 183
Because Runnable only defines one method: run.
So the call:
Thread thread = new Thread(runnable);
thread.run();
Calls the default run() method in the Thread base class, which in turn calls the start() method of the Runnable instance runnable.
HTH :roll: :roll:
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
I just thought it over:
- "extends Thread" => bgr.start() is of type "Background" as bgr is background instance. According to Polymorphism: Whith overridden method the type of the object determines the invoked method: => bgr is of type "Background" hence "Background" start() method is invoked and not the "super.start" of Thread class!
- "implements Runnable" => new Thread(bgr).start() => Instance of Runnable => according to polymorphism => start() method of Runnable is invoked and Thread is startet.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding start() in Threads
 
Similar Threads
doubt abt priorities in threads.......
starting a thread in a constructor
Dan's Question : Thread
Thread question
Join() method in Java