For the sake of an exercise, say you have an input file with a series of lines of text with the objective of reversing their respective character sequences.
Now introduce 5 threads that will each do the reversing, thread 1 taking care of line 1, thread 2 taking care of line 2 and so on.
If the aim is to save these reversed lines in order, how would you save them to the same file ?
P.S: I’ve thought of having some kind of queue but there is no guarantee on the order. Also, I’m wondering if there’s some way to lock the file temporarily and have a thread wait it’s turn. I imagine I should keep track of which line is being saved.
12
What you’d need for a problem like this is a sliding window to hold your output.
Lines are sequentially passed to your threads to be processed and, at the same time, assigned a same-numbered slot in the window. When a line comes back, the result is placed into its numbered slot. When the lowest-numbered slot in the window is filled, you emit its line to the output and slide the window up by one slot, repeating until you reach a slot that doesn’t have a processed line in it. Since you’ve now got one or more threads free, you can start them processing the next set of lines for the slots you just opened up by moving the window.
How you size the window will depend on how you handle your threads. You might have one thread chewing on a very long line at the bottom of the window while others work on a bunch of short ones, which will bring everything to a stop if all of the other slots in the window are filled. Your choices are to either grow the window (which is acceptable) so you can keep your threads occupied or do nothing until the thread working on the low slot finishes.
you need what I would call a producer-transformer-consumer solution
you can adapt an existing produce-consumer but each element has a complete
flag
there will be 3 pointers: one for the head; one for the tail and one for the “stomach” which point to the next element that needs transforming
the tail can’t advance past a element that doesn’t have the complete
flag set,
the stomach pointer advances as the concurrent threads pull elements (without removing it from the queue) to transform and sets the complete flag only when the transformation is done
I put this out completely without comment. If you don’t understand what it’s doing, feel free to ask. If you’re planning to submit it as an interview response, be prepared to discuss.
public class SequencedThread {
private final Runnable runnable;
private final SequencedThread predecessor;
public SequencedThread(Runnable runnable, SequencedThread predecessor) {
this.runnable = runnable;
this.predecessor = predecessor;
}
public void run() {
try {
if (predecessor != null) {
predecessor.join();
}
}
catch (InterruptedException ex) {
// do something here
}
runnable.run();
}
}
7