Mutithreaded Pipeline in Java

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

Say the conveyor entry:

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

If I1 is retrieved, it will be ready for decoding, but the fetch operation will not wait for the decode task to decode . After transferring the extracted instructions to decode , the fetch operation will now receive the next I2 instruction, etc.

This is five-step pipeline planning.

How to simulate such a machine using java multithreading?

+4
source share
1 answer

Assuming you want to know how to implement such a thing: it is called a "pipeline pattern". If this is not homework, you can reuse the existing implementation of this template. One of them is available at:

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

If this is homework, then your teacher can expect you to write it yourself from scratch. A good starting point is this two-stage pipeline (where one thread reads lines from a file and another stream prints lines):

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

In the above example, the two steps interact through a BlockingQueue (i.e., step 1 writes to the queue, and step 2 reads it). If stage 1 is consistently faster than stage 2, the queue will be quite large). You can activate the steps in lockstep using the SynchronousQueue instead [see Comment # 1 on this answer, for what].

If you need a five-stage conveyor, you need to expand it with 5 threads that have 4 queues between them:

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

Above, each [s*] represents a stage (stream), and each qAB represents a queue ending in [sA] and unloaded from [sB]

+11
source

Source: https://habr.com/ru/post/1397822/


All Articles