• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

target Runnable of a thread

 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
You may have just won ten million dollars! Or, maybe a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic