- Spin: Capture until (the current time plus the suspend time in ms.) and loop until the current time is equal to or greater than until.
- Sleep: Call Thread.sleep(
) - Join: Call Thread.currentThread().join(
).
Sleep lost the most precision with 1,358 ms. worth of latency and a cost of 31 ms. of CPU time.
Join seems to be the best compromise with zero CPU utilization and only 7 ms. loss of precision.
So the moral of the story, it seems to me, is to always use Join unless you need a hyper-precise wake-up time. Furthermore, it would seem that Sleep is the worst way to achieve a suspension with much lower precision than Join at an infinitely higher CPU cost.
Output:
SpinLock vs. Sleep vs. Join
Warming Up....
Warm Up Complete
Starting Spin Test
Spin: 500000/499905204500
Starting Sleep Test
Sleep: 501358/31200200
Starting Join Test
Join: 500007/0
Warming Up....
Warm Up Complete
Starting Spin Test
Spin: 500000/499905204500
Starting Sleep Test
Sleep: 501358/31200200
Starting Join Test
Join: 500007/0
==== Update ====
Tested the exact same code using java version "1.6.0_24" on Ubuntu 10.10 X64 and the results were a little different. The Spin did have some precision loss (but not much). The biggest difference was that Join went from zero CPU cost to 3,970 ms cost, making it more expensive than Sleep and with slightly less precision. Not sure what to make of that, except that I need to rerun the tests to make sure there is no skew from background processes or something.
SpinLock vs. Sleep vs. Join
Warming Up....
Warm Up Complete
Starting Spin Test
Spin: 500034/499590000000
Starting Sleep Test
Sleep: 516430/3680000000
Starting Join Test
Join: 516650/3970000000
No comments:
Post a Comment