• 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??

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one basic question what is this JLS?
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Java Language Specification
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic