Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multithreaded Pipeline in Java

I have a problem regarding running multiple threads executing a pipeline in Java.

Say the 'input' of the pipeline is:

5 instructions namely: I1, I2, I3, I4, I5

If I1 has been fetched it will be now ready for decoding but fetch operation will not wait the decode task to finish. After transferring the fetched instructions to decode, the fetch operation will now get the next instruction I2, and so on.

It is a pipelining scheduling with five stages.

How do I simulate such a machine using java multithreading?

like image 807
Celine Avatar asked Dec 22 '22 01:12

Celine


1 Answers

Assuming you want to know how to implement such a thing: It is called a 'pipeline pattern'. If this is not homework, you could reuse an existing implementation of this pattern. One is available at:

http://code.google.com/p/pipelinepattern/

If this is homework, then your teacher might be expecting you to write it on your own from scratch. A good starting point is this two stage pipeline (where one thread reads lines from a file, and the other thread prints the lines):

http://rosettacode.org/wiki/Synchronous_concurrency#Java

In the above example the two stages communicate via a BlockingQueue (i.e. stage 1 writes to the queue, and stage 2 reads it). If stage 1 is consistently faster than stage 2, the queue will get quite big). You can enforce operation of the stages in lockstep by using a SynchronousQueue instead [see comment #1 to this answer, for why].

If you need a five stage pipeline, you need to extend this by having 5 threads which have 4 queues between them:

in -> [s1] -> q12 -> [s2] -> q23 -> [s3] -> q34 -> [s4] -> q45 -> [s5] -> out

Above, each [s*] represents a stage (a thread) and each qAB represents a Queue being enqueued to by [sA] and dequeued from by [sB]

like image 195
ArjunShankar Avatar answered Dec 23 '22 15:12

ArjunShankar