Busy Spinning or Busy wait in multi-threading

Busy spinning or busy wait is a technique in a multi-threaded environment, where other threads loop continuously waiting for a thread to complete its task and signal them to start.

When one thread loops continuously waiting for another thread to signal.

This technique can be used whenever there is requirement to execute second thread (say ConsumerThread) after completion of first (say ProducerThread ) thread.

1
2
3
4
5
6
7
//inside ConsumerThread's run method

while(this.producerThread.productionInProcess)
{
System.out.println("ConsumerThread is waiting for ProducerThread to get over");

}

Traditional threading methods like sleep(), wait () & notify() releases the CPU but busy spin does not relinquish CPU and hence preserves CPU caches. We must use sleep() or wait() and notify() method. Using wait() is better option.

In ProducerThread we can have a boolean flag variable as productionInProcess which can be set to true in ProducerThread constructor and can be set to false once ProductionThread reaches to completion of its task.

In ConsumerThread in side the run method we will have a while loop which keep checking the value of ProducerThread’s  productionInProcess  variable and once this variable becomes false (as ProducerThread is completed it work) ,ConsumerThread can start it own working .

 

Complete Code Example – Implementation of Producer and Consumer using Busy Spin mechanics

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.util.ArrayList;

/* Producer will allow consumer to
* consume only when 100 products have been produced (i.e. when production is completed).
*/

class ProducerThread implements Runnable{

boolean productionInProcess;
ArrayList<Integer> productList;

ProducerThread(){
productionInProcess=true; //initially ProducerThread will be producing, so
//make this productionInProcess true.
productList=new ArrayList<Integer>();
}

@Override
public void run(){

for(int i=1;i<=100;i++){ //Producer will produce 10 products
productList.add(i);
System.out.println("ProducerThread is Producing, Product id#: "+i);

try{
Thread.sleep(100);
}catch(InterruptedException e){e.printStackTrace();}

}
productionInProcess=false; // Once production is over, make
//this productionInProcess false.
//Production is over, consumer can consume.

}

}

class ConsumerThread extends Thread{
ProducerThread producerThread;

ConsumerThread(ProducerThread pt){
this.producerThread=pt;
}

public void run(){
/*
* consumer will continuously loop until productionInProcess is true.
*/

while(this.producerThread.productionInProcess){ //BUSY SPIN
System.out.println("ConsumerThread waiting for production thread to get over.");
}

/*productionInProcess is false means production is over,
consumer thread will start consuming. */

System.out.println("ProductionThread is over, consumer can consume.");
int productSize=this.producerThread.list.size();
for(int i=0;i<productSize;i++)
System.out.println("Consumed : "+ this.producerThread.list.remove(0) +" ");

}

}

/** Copyright (c), VivekPanday UpsilonWin */
public class BusySpinDemo{
public static void main(String args[]){

ProducerThread pt=new ProducerThread();
ConsumerThread ct=new ConsumerThread(pt);

Thread prodThread=new Thread(pt,"prodThread");
Thread consThread=new Thread(ct,"consThread");

prodThread.start(); //start producer thread.
consThread.start(); //start consumer thread.

}

}

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *