I guessed it. I wasn�t sure. I want to clarify: 1. When anonymous class is created it will print "executing thread run" and later on will call constructor of thread. 2. When you say t.start(), it will call its own start and run. It won�t see for run defined here. And run() just satisfies the interface runnable.
"Kalyani", please take a little time to read our JavaRanch Naming Policy and change your displayed name to conform with it. In your case we need two names in the format <first name> <family name> (and in that order). Thanks -Barry
[Jeroen]: The short (and only needed) answer is that Thread implements Runnable and Runnable defines run() therefore Threads needs to implement run().
But the Thread class already does implement run(). If run() had not been overridden, that's the version of run() that would have ran. Since run() was overridden, the default behavior of Thread's run() method did not get called, and the overriding method (in the anonymous class) did get called[/i]. So consider: what would have happened if the anonymous class did not override the run() method? And then, since run() actually is overridden, how did that change the result?
"I'm not back." - Bill Harding, Twister
Joined: Oct 12, 2000
If a Thread is created to use a Runnable the run() method from that Runnable will be used instead of the run() method of the Thread itself (or rather the run() method of the Thread() will call the run() method of a passed Runnable(). This becomes readily apparent when investigating the code for Thread which either calls a run() method on a supplied Runnable or terminates immediately.
If run() in Thread is overridden to provide different behaviour and the superclass run() isn't called the run() from the Runnable may well be ignored completely (depending on the specific implementation of run() in the Thread subclass.
Given the scenario given, it should therefore come as no surprise that the inclusion of a Runnable implementation in the Thread constructor will have had no influence on the outcome of the Thread's start method. That is of course, unless start() itself is overridden to ignore the run() method in the class itself and use that of the subclass (which would require implementing native code to replace the default multithreading code, probably outside the scope of the OPs question).
Joined: Mar 04, 2005
Here is the code other way around where I override runnable.
The output is :
executing thread run
Which means that the thread object first looks at its own run method and if doesn't find out, will look at the runnable object's.
Joined: Mar 04, 2005
This program will make it even more clear:
I have overriden the thread's run and am calling super.run() inside.
Joyce [ April 14, 2005: Message edited by: Joyce Lee ]
Joined: Apr 08, 2005
Thread-1, 5 and main represent the thread's name, priority and thread group respectively.
Thank you, Joyce. I just want to know the run method in my class overrides the run method in Threads or Runnable class?
Joined: Jul 11, 2003
Lauren: I just want to know the run method in my class overrides the run method in Threads or Runnable class?
The run() method in StartInThreadAndRunable overrides the one in the Thread class.
By studying the source code of the Thread class, you'll have a better understanding of how it works. The source code is available in the src.zip under your JDK-xxx directory.
Below is a small portion of Thread's source code:
A Runnable object that passed into a Thread constructor will be stored in the target variable. The Thread's run() will in turn call the target.run(). So if run() in Thread class is overridden, target.run() will not be called.
Here is a simple example:
The output is "ThreadTest: run" because run() is overridden in the ThreadTest.
If you comment or remove run() in ThreadTest, the run() in Thread class will be called which in turn calls the target.run(). So the output will be "RunnableTest: run".
Joyce [ April 15, 2005: Message edited by: Joyce Lee ]