I have 6 functions:
Each giving input to another. I want to execute them at the same time i.e., pipelining.
How to do that?
Create a class for each pipeline component that implements Runnable. Give each component a ConcurrentLinkedQueue to hold the data to be processed; each component will poll this queue in an infinite loop (in its run() method), processing the data as it pulls it off. Each preceding component will add its output to the next component's queue. Now assign each runnable to a thread, start the threads, and start feeding data to the first component's queue.
If a component finds that its queue is empty then you may want to put it to sleep for half a second or so.
You may also want to add a cancel() method to each component that will break out of the infinite loop in its run() method.
public class Decode implements Runnable {
private boolean cancel = false;
private ConcurrentLinkedQueue<Data> queue = new ConcurrentLinkedQueue<>();
private FetchOperands nextComponent;
public void run() {
while(!cancel) {
Data data = queue.poll();
if(data != null) {
...
nextComponent.enqueue(data);
} else (Thread.sleep(500);
}
}
public void enqueue(Data data) {
queue.offer(data);
}
public void cancel() {
cancel = true;
}
public void setFetchOperands(FetchOperands nextComponent) {
this.nextComponent = nextComponent;
}
}
public class Main implements Runnable {
public void run() {
Decode decode = new Decode();
FetchOperands fetchOperands = new FetchOperands();
decode.setFetchOperands(fetchOperands);
Thread t1 = new Thread(decode);
Thread t2 = new Thread(fetchOperands);
t1.start();
t2.start();
t1.join();
t2.join();
}
}