Hi Marlene, My guess is that the start() method of the Thread class will always invoke its own run() method. If you do not override it, and if you pass a Runnable object to its constructor, then that's the time when the run() method of the Runnable object is called.
Hi Marlene My guess: I think that the run() method of the orignal Thread class is never being invoked what instead is happening is that you are invoking the overridden run() method of the class. Hence you are not getting the expected output. So in case the class itself has a run() method then that will be having a higher priority than the target.run() method.
You have extended the Thread class and overrided the run method, so when you call start() on object A so the JVM opcode invokevirtual will choose the run method from object A, that will become most specific method in current context. Anupam there is nothing like priority of a method in java.
Joined: May 30, 2003
I belive the question was why the run() method of the Thread object, overridden or not, that is called instead of the the run() method of the Runnable object ? Correct me if I am wrong here. [ August 02, 2003: Message edited by: Alton Hernandez ]
target is a Runnable object that was passed in. The run() method of thread will determine if the Thread object has been passed a Runnable. If it has been then it runs the run() method of the Runnable object. If it hasn't been it simply ends. Marlene has overridden the Thread's run() method so it will never make this determination and will never run the run() method of any Runnable object passed to it.
Thank you very much Alton, Anupam, Satyendra and Thomas for thinking about my problem and for contributing your ideas and explanations. Yes, it is important to see the source code and to know that Thread.run() invokes target.run(). Thread.run() is overridden by A.run(). We know the start method does not invoke the run method. The start method is executed by the main thread, whereas run is executed by the new thread Thread-1. When the scheduler selects the new thread as the current thread, somewhere in the virtual machine a run method is invoked. We know the ThreadGroup object has references to all of its Thread objects, hence to the A object. My guess is the virtual machine might be doing something like this. Thread currentThread = threadGroup.thread[i]; currentThread.run(); It follows, since the actual type of the object referenced by currentThread is A, the run() method of Thread is overridden by the run() method of A. (Except that the virtual machine might be written in C or C++. Hmmm, I wonder how they do polymorphism in C?) [ August 02, 2003: Message edited by: Marlene Miller ]
Joined: Apr 13, 2003
Originally posted by satyendra adhikari: You have extended the Thread class and overrided the run method, so when you call start() on object A so the JVM opcode invokevirtual will choose the run method from object A, that will become most specific method in current context. Anupam there is nothing like priority of a method in java.
Satyendra I was aware of that. I made that statement just to make myself clear. There isn't any actual priority but when you have a run() method in a class then it be having a higher priority than the Runnable's run() method. Here by priority I don't mean the java (MAX/MIN_PRIORITY) priority. [ August 02, 2003: Message edited by: Anupam Sinha ]