userimage

what is busy spinning in java

what is busy spinning in java
userimage
Moe
Busy spinning, or busy wait, is a programming technique where you test something repeatedly and do nothing until the test passes. For example in Java:

while (!someResource.IsAvailable()){
    // Do some operation..
}

// If you get here, you know that someResource.IsAvailable() returned true

A busy spin is a primitive form of synchronization; when you leave one you know some test has passed (in the above case, someResource.IsAvailable() was once true).

Usually busy spins are wasteful of CPU cycles and should be avoided, for example in Java you would typically use a synchronized block instead; your code will be "frozen" and not use CPU cycles until the situation you are waiting for is satisfied.

However, synchronized blocks take a bit of setup cycles and require runtime support. If the thing you are testing is almost always satisfied, and if not satisfied will always be satisfied very soon, a busy spin may use fewer cycles than proper synchronization.
Answer is