Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread sleep  RSS feed

 
badrinarayanan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically a thread sleeps to allow the other threads to make use of the CPU time..
Suppose if a thread accesses a critical region ...
In the case of thread.sleep() it does not relinquish the lock but it sleeps for some time....

what is it's use if other threads are not able to access the critical region in the mean time? or what does the thread do while sleeping..
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Srinibadri -

Welcome to JavaRanch!

On your way in, you may not have noticed our naming policy, which requires you to use both a first and last name. You can change your display name here. Thanks, pardner!

Now, as to your question: yes, sleep() doesn't relinquish any locks that the Thread might be holding. Sometimes, that's just what you want, but other times, of course, it's not. For those other times, there is Object.wait(), which is something like sleep, but it releases a lock held by the thread.

How to use wait() (and its companion notify()) is a large topic; you should go read Sun's threads tutorial, ans especially the section on "Synchronizing Threads."
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are some examples where you might have a separate thread that does something then sleeps for a while, repeatedly.

You might have a GUI that has a clock. Your program starts the clock in a separate thread. The thread displays the time then sleeps. When it wakes up, it cannot just add 1 second, because in reality it might have slept more than 1 second. So it gets the time from some reliable source and displays the time. Then it goes back to sleep. et cetera.

Or you might have a GUI with an animation of a bicycle moving across the screen. Your program starts the animation in a separate thread. The thread displays the bicycle, then sleeps 1 tenth of a second. It wakes up, rotates the wheel a little and moves the bicycle forward a little. Then it goes back to sleep. et cetera.

Or you might have a program that has a network connection to a program on some other computer. Suddenly the line goes down and your program gets an error message. Your program tries to reconnect and fails. Your program sleeps for 1 minute, wakes up and tries again to reconnect. Eventually some one fixes the line. Your program wakes up and connects to the other program.

Or you might have a program that has many tasks to do at specific times of the day. Each task runs in a different thread. The main thread has a queue of tasks ordered by the time of day the task starts. The main thread looks at the next task. It starts in 10 minutes. So the main thread sleeps for 10 minutes, then wakes up and starts the task. The next task starts in 5 minutes. So the main thread sleeps for 5 minutes, then wakes up and starts the task.

I've never seen an example in the real world where a thread locks a block of code then goes to sleep.
[ June 04, 2004: Message edited by: Marlene Miller ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!