• 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

How Thread.sleep works...

 
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok... lets say I'm executing through a thread and I'm looking to make a thread sleep for say 2 seconds. I put in the following line...

Thread.sleep(2000);

Given that the sleep method is static, how does the class know what specific thread to sleep on? Does it pass along the process id?

TIA
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The API documentation for that method says this:

"Causes the currently executing thread to sleep..."

Does that answer your question?
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hehe.. I guess. Wasn't sure how it knew what thread was executing however I guess I'll trust the API like I usually do. It must pass something behind the scenes in order to figure out what thread is executing since there is no thread you pass in and the method is static.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dale DeMott:
It must pass something behind the scenes in order to figure out what thread is executing since there is no thread you pass in and the method is static.

Not quite true, the current thread is the thread that is executing the sleep method. You don't need to pass a reference to it into the method as it's already there eg it's the thread that's doing the work.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[I wrote this before seeing Tony's response - Jim]

To put it another way - whatever thread is executing the code that called this method is the one that's currently executing at the moment the method is executed. So whatever thread calls sleep, that's the one that sleeps.

Also, note that there's another static method

Thread.currentThread()

Thus, it's always possible to discover the currently-executing thread. There's no extra data that needs to be passed around. It's part of the built-in capabilities of the JVM, implemented via native code. The JVM already has to have a thread scheduler capable of keeping track of multiple threads and letting each one run in turn - and as long as they have that, then it's relatively trivial to provide a method to identify which thread is currently running.
[ August 07, 2007: Message edited by: Jim Yingst ]
reply
    Bookmark Topic Watch Topic
  • New Topic