• 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

A Thread puzzle

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all!
In reviewing my notes I came across something that seemed a little strange: 1) The Yield method is a STATIC method of the Thread class. 2) This method causes the CURRENT thread that is running to give up the CPU.
Is it just me, or does there seem to be some tension between these two statements? If the Yield method is STATIC then it operates at the CLASS and not the INSTANCE level. i.e, there is not implicit 'this' reference. So if it operates at the class level, then how the heck can it figure out which Thread instance it is supposed to operate on?
Dan
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The current thread is the one that's executing the yield statement. If it weren't the current thread, the method would not be executing. Q.E.D. I know it seems weird at first, but think about it some more and it should gradually make more sense to you.
The method is made static to discourage people from trying something like
<code><pre> someOtherThread.yield();</pre></code>
which can't possibly affect any thread other than the one that's executing. (Technically the above code is still legal as long as someOtherThread is declared as type Thread - but since yield() is static it has the exact same effect as Thread.yield(), and no effect on someOtherThread.)
 
Dan Temple
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay ... after smashing my head in with a large cinder block I think I understand. Because it is a thread, the method knows which thread to affect WITHOUT using an instantiating object that refers to that thread. Namely it uses whatever thread is running the method. It is sort of a self-referential situation that causes eyes to explode... correct?
Dan
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup.
 
Dan Temple
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay! Thanks for the info ... now I'll just have to regrow some new eyes ...
Dan
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic