jQuery in Action, 2nd edition
The moose likes Threads and Synchronization and the fly likes Should a thread always sleep? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Should a thread always sleep?" Watch "Should a thread always sleep?" New topic
Author

Should a thread always sleep?

Daniel Pride
Greenhorn

Joined: May 09, 2008
Posts: 11
I'm just wondering, is it necessary for a thread to sleep, lets say after every iteration of an operation or calculation it is doing?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"indigo0086",
Please check your private messages regarding an important administrative matter.
-Ben


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by indigo0086:
I'm just wondering, is it necessary for a thread to sleep, lets say after every iteration of an operation or calculation it is doing?


No, it's not necessary, and it would usually be wrong.

Modern operating systems have pre-emptive threading, which means that the CPU will switch between threads periodically, without the threads having to relinquish control explicitly.

If you want to do a calculation "in the background", without slowing down other processes too much, use thread priorities to achieve it. That way, if no other thread wants the CPU, your calculation gets the whole CPU; with sleeping, it won't, and time will be needlessly wasted.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
To add to what Peter said, if you are really in a situation where you want to tell the operating system "now would be a good time to see whether a different thread wants to do something", use Thread.yield().


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Daniel Pride
Greenhorn

Joined: May 09, 2008
Posts: 11
Originally posted by Peter Chase:

If you want to do a calculation "in the background", without slowing down other processes too much, use thread priorities to achieve it. That way, if no other thread wants the CPU, your calculation gets the whole CPU; with sleeping, it won't, and time will be needlessly wasted.


If I write a simple test setting three test thread priorites, say one thread with a high priority and another with the same priority, and the last with a bit lower priority, then without yield or sleep, each thread will just continue until finished, no time slicing out.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Daniel:
then without yield or sleep, each thread will just continue until finished, no time slicing out.


How do you determine that there has been no time slicing?
OS will always give you an impression that all the threads are running simultaneously.
This is *just* an "impression" that thread scheduling is "supposed" to create.
Of course unless you are having the number of processors equal to number of threads.


apigee, a better way to API!
Daniel Pride
Greenhorn

Joined: May 09, 2008
Posts: 11
What gave me the impression was I had four threads, all of which basically did a simple count to 100, no sleeping or yielding, and the only thing that priority dictated was which one finished first, but there was no interleaving of output. Does this only indicate that short-running threads don't show signs of time slicing beyond when the thread actually starts executing.

For example, this simple Word file counter



If I had 4 or 5 threads with this instruction, would they properly share time between them, or would they just execute until completion depending on priority?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35241
    
    7
A loop to 100 isn't a very good way of testing concurrency, because it's so short. The first thread may be finished before the second one ever gets started.

It's easy enough to test what this code would do, but it probably has a better chance of seeing interleaving, since there's some I/O (which takes up a loooong time compared to counting to 100).
[ May 12, 2008: Message edited by: Ulf Dittmer ]

Android appsImageJ pluginsJava web charts
Daniel Pride
Greenhorn

Joined: May 09, 2008
Posts: 11
Ok cool, I thought maybe it was to show which is why it was giving me the illusion of threads running sequentially rather than concurrently.
[ May 12, 2008: Message edited by: Daniel Pride ]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Should a thread always sleep?
 
Similar Threads
Blocking a thread
The java desktop application if hang up when not using for a period of time (about 30 minutes)
Threads Mock
wait() Vs Thread.currentThread().wait()
Passed 1.4 with 67%