aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes target Runnable of a thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "target Runnable of a thread" Watch "target Runnable of a thread" New topic
Author

target Runnable of a thread

Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Here is an example to check your understanding of threads. When a new thread is created, the run method of the target Runnable object is not invoked.
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
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.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
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.
satyendra adhikari
Ranch Hand

Joined: Aug 17, 2001
Posts: 52
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.
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
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 ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Here is the Thread's run() method:

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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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 ]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: target Runnable of a thread