• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sleep??

 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
Gaurav Chhabras
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
rekha devan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one basic question what is this JLS?
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java Language Specification
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic