top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Java: What is busy spin in multi-threading? [CLOSED]

+2 votes
8,033 views
Java: What is busy spin in multi-threading? [CLOSED]
closed with the note: None
posted Feb 23, 2016 by Karthick.c

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

+1 vote
 
Best answer

When one thread loops continuously waiting for another thread to signal, the condition is called busy spin. It simply eats up the computer cycle because one thread keeps on looping continuously waiting for another thread to signal.

Example Case:

while(this.prod.productionInProcess){ 
  System.out.println("BUSY SPIN - Consumer waiting for production to get over");
}

How to avoid the Busy Spin
Performance is improved because consumer thread called wait() method. Once production is over, producer thread will call notify() method, which will notify consumer thread and consumer will start consuming. So, consumer thread will not consume CPU unnecessarily.

answer Feb 24, 2016 by Salil Agrawal
+1 vote

Some of the other answers miss the real problem with busy waiting.

Unless you're talking about an application where you are concerned with conserving electrical power, then burning CPU time is not, in and of itself, a Bad Thing. It's only bad when there is some other thread or process that is ready-to-run. It's really bad when one of the ready-to-run threads is the thread that your busy-wait loop is waiting for.

That's the real issue. A normal, user-mode program running on a normal operating system has no control over which threads run on which processors, a normal operating system has no way to tell the difference between a thread that is busy waiting and a thread that is doing work, and even if the OS knew that the thread was busy-waiting, it would have no way to know what the thread was waiting for.

So, it's entirely possible for the busy waiter to wait for many milliseconds (practically an eternity), waiting for an event, while the the only thread that could make the event happen sits on the sideline (i.e., in the run queue) waiting for its turn to use a CPU.

Busy waiting is often used in systems where there is tight control over which threads run on which processors. Busy waiting can be the most efficient way to wait for an event when you know that the thread that will cause it is actually running on a different processor. That often is the case when you're writing code for the operating system itself, or when you're writing an embedded, real-time application that runs under a real-time operating system.

Kevin Walters wrote about the case where the time to wait is very short. A CPU-bound, ordinary program running on an ordinary OS may be allowed to execute millions of instructions in each time slice. So, if the program uses a spin-lock to protect a critical section consisting of just a few instructions, then it is highly unlikely that any thread will lose its time slice while it is in the critical section. That means, if thread A finds the spin-lock locked, then it is highly likely that thread B, which holds the lock, actually is running on a different CPU. That's why it can be OK to use spin-locks in an ordinary program when you know it's going to run on a multi-processor host.

answer Feb 24, 2016 by Rajan Paswan
0 votes

Busy spin is one of the technique to wait for events without releasing CPU. It's often done to avoid losing data in CPU cached which is lost if the thread is paused and resumed in some other core. So, if you are working on low latency system where your order processing thread currently doesn't have any order, instead of sleeping or calling wait(), you can just loop and then again check the queue for new messages. It's only beneficial if you need to wait for a very small amount of time e.g. in micro seconds or nano seconds. LMAX Disrupter framework, a high-performance inter-thread messaging library has a BusySpinWaitStrategy which is based on this concept and uses a busy spin loop for EventProcessors waiting on the barrier.

answer Oct 12, 2016 by Arun Angadi
...