• 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

Why are sleep and yield static?

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I posted this question at Marcus's web site and I thought I'd post it here too. Why are the yield and sleep methods of the Thread class defined as static as opposed to being instance members? Thanks in advance. Regards, Joe
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you call the yield or sleep method, it applies to whichever thread is currently executing, rather than any specific thread - you don't have to specify which thread is currently running to free up the processor. Don't know if that explains it!
[This message has been edited by Lucy C (edited March 08, 2000).]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm moving this to Java in General (Intermediate) as it falls outside the scope of the exam.
 
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
A good question. Here are my thoughts:
In the case of yield:
At any given time, there is only one thread that could possibly yield, and that's the thread that's currently executing. If you had two threads t1 and t2, and from t1 you called t2.yield(), what could that possibly accomplish? The code would only execute when t1 was executing, in which case telling t2 to yield would be pointless - it's already yielded. So since the only thread worth calling yield on is the current thread, they make the method static so you won't waste time trying to call yield on some other thread.
In the case of sleep:
My guess is that sleep as an instance method would lead to deadlock problems similar to those of the now-deprecated suspend() method. Check out "Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?". Problems with sleep() wouldn't be as permanent, perhaps, as the sleep() time limit would eventually expire, but you could still paralyze the JVM until that happens. I suppose you can do that anyway if you're careless, but I think the key is that if a thread is holding lock to critical resources, you should know not to call sleep until those locks are released. If you do it anyway, it's your own fault. But if another thread were able to invoke sleep on this thread, it would have no way of knowing what section of code was being executed, and whether or not it's a good time for it. In general, it's better to have threads put themselves to sleep. If you want one thread to be able to tell another to go to sleep, you need to devise a means of communication between the threads, and have the target thread check periodically to see if it's been requested to go to sleep. See the section on "What should I use instead of Thread.suspend and Thread.resume?" in the previous link for a related example.
In both cases:
I think using static methods makes these slightly easier to use, too. If you're in a class which does not have a reference to the current Thread instance, it would take longer to invoke these methods as instance methods:
<code><pre> Thread.currentThread().yield();
Thread.currentThread().sleep(1000);</pre></code>
instead of:
<code><pre> Thread.yield();
Thread.sleep(1000);</pre></code>
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting analysis, thanks for your insights. Regards, Joe
 
reply
    Bookmark Topic Watch Topic
  • New Topic