GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes sleep?? 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 "sleep??" Watch "sleep??" New topic
Author

sleep??

srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
a simple doubt i got is ...
like sleep ,yield is also static..can we use like
sleep(100)....how reasonable it is ???i got this doubt while doing this ...


Thanks & Regards<br /> <br />-Srikanth
Gaurav Chhabras
Ranch Hand

Joined: Sep 21, 2005
Posts: 126
In generic terms when you call sleep(1000) this means that the thread on which you are executing it will sleep down for 1000 milleseconds, go to sleep state and then go to Ready to Run state after the defined time in the constructor and when you will called yield methord then the thread will immediately go to Ready to Run state and wait for it chance to be get executed.

In Nutshell -:

***sleep(1000) -: immediately get the current running thread blocked and go to Ready to Run state after the defined time in the constructor and wait for its cance to be get executed.

***yield -: immediately take the thread on which it is called to Ready to Run state and wait for it chance to be executed , it does not get Blocked.
Kris Krason
Greenhorn

Joined: Aug 05, 2005
Posts: 25
Could you refraze your question, I don't get it.
Are you asking what sleep() does ?

BTW. In your code you should replace calling run() method with start() if you want those two classes to run in seperate threads and not the one that started them.


Kris Krason<br />SCJP1.4 (96%)<br />SCJP1.5 (91%)<br /> <br />See my <a href="http://krisreviews.com/2006/12/code-complete-second-edition/" target="_blank" rel="nofollow">Code Complete review</a><br /> <br /><a href="http://www.coderookie.com/2006/tutorial/the-pseudocode-programming-process/" target="_blank" rel="nofollow">Pseudocode Programming Process</a>
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
krason the code is ok and i got the o/p one one two two....thats fine....
but my doubt is can we call the sleep method directly like
sleep(1000);
but sleep and yield method are static methods present in Thread class...

thanks
sri
Kris Krason
Greenhorn

Joined: Aug 05, 2005
Posts: 25
Yes it should be correct. JLS states that a class inherits all super class methods that are not private (and with package for default).
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
Hi Srikanth,

You can call Thread.sleep() or Pmcraven.sleep() or sleep() directly because you do it in a Thread class.

The preffered way is to call Thread.sleep() because it's a static method.

But I don't see why Pmcraven extends Thread... Is it a mock exam question ?
I say that because like Krzysztof says, there's always only one thread running during the exucution.


Regards,<br />Seb<br /> <br />SCJP 1.4
rekha devan
Greenhorn

Joined: Oct 10, 2005
Posts: 26
one basic question what is this JLS?
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
The Java Language Specification
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Seb Mathe:
... The preffered way is to call Thread.sleep() because it's a static method...

Yes, and it acts on the currently executing thread, without regard to any instance of Thread that might have been used to call the method.

For example, if threadA is running and calls threadB.sleep(), it's threadA that sleeps -- not threadB.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
GeeCON Prague 2014
 
subject: sleep??