The run() method of the Runnable, used to create a thread, is called from the run() method of the java.lang.Thread class. Since, you have overridden the run() method of Thread in your MyThread class and have not called super.run(), so there is no way the run() method of your runnable class will be executed. If you just want to execute the run method of the Runnable instance then you should not override the run() method in MyThread class.
I knew that what i wrote made little sense ... yeah sometimes i fail to explain clearly Okie, so when you call start on a thread instance, after the JVM does all the stuff required to create a thread, it will call the run() method of the thread instance. Now, in your implementation, MyThread, you have overridden the run() method. So, the JVM will call this run() method implemented by you instead of run() method in java.lang.Thread. Also, you have not called super.run() so the code in the parent class (java.lang.Thread)will never be called. agree? Since, run() in the parent class is not called, so, no one is really aware of the Runnable instance passed to the constructor and hence it does not get executed. However, when you directly instantiate java.lang.Thread using a Runnable, when this thread starts, the run() method in java.lang.Thread gets called and in turn it calls the run() method on the runnable instance initially passed(as you can see in the code.) Hope it is clear
Joined: Feb 28, 2007
Also, you have not called super.run() so the code in the parent class (java.lang.Thread)will never be called. agree?
You are giving more emphasis to this super.run(). Could you please give me a little example when you call super.run() and make my MyRunnable instance's run method to be called when I do this :
1- You created a class that extends Thread; Thread implements Runnable is known fact. I had overridden the run() method of the Thread class in the MyThread class. 2- When I called start() method on the MyThread instance (constructed using MyRunnable), because I didn't call super.run() (in my previous post) to tell that Thread has to call the run() method of the Runnable interface that is passed to it (simply when you pass Runnable to the Thread constructor, you are passing a Runnable implementor that overrides the Thread's run() method, so therefore the Runnable's run() method is called.)
What when I didn't call super.run(), Here I would have to disagree with Nitesh that Thread has no knowledge of the Runnable Thread has been constructed using MyRunnable, so it knows it.
When you construct a Thread object using the constructor that takes Runnable, because Runnable overrides the run() method, it's run method is called.
No, its not because runnable overrides the run method(infact it does not override it defines.), it's run method is called. By constructing the Thread instance passing runnable you tell the instance to call the run on the runnable. This will work only if you let the run() method of Thread execute, but, you are not allowing this method to execute as you have overridden it in your implementation i.e. MyThread. This is the reason why, i suggested to see the implementation of java.lang.Thread.run(). I thought it will give you a clarity of the same. That method is the only place where the delegation to the passed runnable instance is done. If this method does not execute, the runnable run method will not be executed.
The whole story came to my mind when I went through your following lines
This is the reason why, i suggested to see the implementation of java.lang.Thread.run(). I thought it will give you a clarity of the same. That method is the only place where the delegation to the passed runnable instance is done. If this method does not execute, the runnable run method will not be executed.
And specially If this method does not execute, the runnable run method will not be executed.
Who said so ? It is a compiler error. You must define the no-arg constructor if you have defined any another constructor in the class. No-arg constructor will be added by the compiler only if there are no other constructors defined in the class.
I dont require MyThread() constructor (default constructor), so I didn't define it. I needed a constructor that require Runnable so I defined that.
But you were using it [ April 09, 2007: Message edited by: Nitesh Kant ]
Joined: Feb 28, 2007
I overlooked the following line you added to my latest code:
Frankly, i am not considering it as a blunder. I know to err is human and everyone makes mistakes big or small. So do I. In the last comment i was just pulling your leg that you probably felt bad about. Apologies about that.