• 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

Sleep and yield methods

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The sleep and yield methods in Thread Class are defined as static. I was just wondering whether there was any specific reason for these methods to be static.
The static modifier suggests that it does not need an instance of Thread to be invoked, which seems contradictory to the behaviour defined for these methods. One puts a thread to sleep and the other causes a thread to yield, which suggests a change in the current state of an instance. Can someone explain this ?
Thanks
Sajida
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sajida,
Your are right, that they are static methods and are basically useful for threads only. But maybe you overlook that in Java, multithreading is built in at the basic level. So even your "main" method runs as a (user) thread.
So, it is useful for these threads (which do not extend Thread and implement Runnable) to go to sleep if they have nothing to do or temporarily stop running for some other higher priority thread.
Hope that answers your question
Navin.
 
sajida kal
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Navin,
Actually I was not questioning the usage of the methods. Let me frame it again. My question is why are the methods static ? Keeping the definition of a static method in mind, ie static methods do not require an instance to be invoked, these methods do affect an instance.
Is it because sleep() appearing in main by itself would cause the
main thread to sleep, so that is the reason the method is defined as static ? That seems like it. Any comments on this ?
Cheers
Sajida

 
Navin Narayan
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sajida,
Let me answer it this way,
Your question "Why are the methods static"
Let us assume that JavaSoft guys didn't make the method static.
And that you are writing a program. Your program tells the user every one min what the time is.
So, your program will have to sleep for atleast 1 min.
If sleep was not static, your program would have to unnessarily extend Thread just to have that one functionality.
Also, in programs which are CPU intensive, it is a good idea to call yield. But does that mean that all CPU intensive programs have to extend Thread just because of that one fuctionality "Thread.yield()".
Luckily for us, JavaSoft people understood this and made those two methods static. Now whenever, any method calls "Thread.sleep()" or "Thread.yield()", that method rsply goes to sleep or yields.
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think...,
A static method is invoked on behalf of an entire class, not on specific object, you are right these methods do affect an instance, it might perform a general task for all object of the class, such as returning the next available serial number of the object.
this is what Bruce Eckecl said :
"...With the this keyword in mind, you can more fully understand what it means to make a method static. It means that there is no this for that particular method. You cannot call non-static Methods from inside static methods (although the reverse is possible), and you can call a static method for the class itself, without any object. In fact, that�s primarily what a static method is for...."
A good example is Math class :
Math has a private constructor, maybe it's because sun want
to control how an object is created and prevent someone from directly accessing a particular constructor.

But maybe we will make a comment, "hey sun...There�s another effect in this case: Since constructor is defined private, it will prevent inheritance of this class",
sun gives the answer by making Math class final (we cannot sub-classed).
and then another comment : "but how can I access the atribute of Math class, that's why sun make all the Math.method static.
stevie
 
sajida kal
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stevie, Navin
Thanks for your responses.
Stevie,
Yes, it makes sense that methods in Math class are static. It is more intuitive to say abs(int a) where the arguments are primitives instead of having an instance of Math class say math and calling math.abs(int a). It doesn't add much value to have the methods as non-static. The effect of sleep or yield is on
an instance, so I didn't apply the same analogy in this case.
Navin, I thought over your explanation and your reasoning seems correct. Do any of the ranch moderators have any comment to make on this ? Or what we have covered in this discussion so far covers the issue fully?
Thanks everyone.
Sajida
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Salija
I see 2.5-3 good reasons for sleep and yield to be static.
1)
The sleep and yield methods always work on the current thread. Apart from the scheduler, only the thread itself should make it sleep (the suspend and resume methods are deprecated). Therefore, you will never have to call the sleep method on any other instance of a thread than yourself (the running thread).
2)
If you spawn a thread like this:

- then you can only make the thread sleep inside the Foo class if either a) you had a reference to the thread t inside Foo, or b) sleep was static so you could write Thread.sleep(100);
If a) was an option, you could also remotely make threads sleep, which is kind of like remotely suspending it.
3)
Another point (actually, only half a point) is that thread scheduling is low level and likely CPU-intensive. Static methods are fast.

Maybe there are other points?
Cheers, Steffen
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
call sleep() (inside run() for example) equals Thread.sleep() equals this.sleep(); i guess, JVM will take the context of instance of 'this' as arguments which is specific to 'this' instance in manipulating 'this' thread only! a guess ONLY doing this way can avoid doing GENERAL things which might effect other thread as a static method -native method actually, we will no way in knowing what is going on inside indeed, don't u think?rolleyes:
any comments, guys?
Rick
 
Rick Zhong
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


call sleep() (inside run() for example) equals Thread.sleep() equals this.sleep(); i guess, JVM will take the context of instance of 'this' as arguments which is specific to 'this' instance in manipulating 'this' thread only! a guess ONLY doing this way can avoid doing GENERAL things which might effect other thread as a static method -native method actually, we will no way in knowing what is going on inside indeed, don't u think?
any comments, guys?
Rick



[This message has been edited by Rick Zhong (edited April 13, 2001).]
reply
    Bookmark Topic Watch Topic
  • New Topic