Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
"reverse them" - reverse the text in the line ("meht esrever") or reverse the order of the lines as was read in? Part of the challenge of defining this is that reading in lines is done in sequence and isn't practical to multithread. Could you clarify the question to help identify where the threading problem is (and eliminate other possible problems/confusion)? – user40980 Mar 29 '13 at 15:42
It is an interesting problem, but in this specific case, multithreading doesn't seem necessary. Especially if you are going to do locking, so only one thread at a time can do its job. You're not going to get any significant performance gain, but you'll get some overhead from the threads. – Niklas H Mar 29 '13 at 15:46
I doubt you can do that without i) either telling each thread the line number (or some other information, which allows to restore the sequence later on, like some pointer to the next line) it is working on and letting it return that number after it has done it's work or ii) force the threads to do their work one after another, but then you don't want to work with threads in the first place. – Thomas Mar 29 '13 at 16:33
@parsifal: What's being processed doesn't really matter. His question is how to split work linear work among multiple threads while maintaining the sequence of the results. – Blrfl Mar 29 '13 at 17:39
@parsifal: James didn't say reverse the order of the lines, he said reverse the order of the characters in each line. The edits don't show any change from the original question. The real meat of his problem is maintaining sequence when processing an ordered set of items in parallel and the return order is nondeterministic. (James, might I have a stab at editing the question to remove the red herring?) – Blrfl Mar 30 '13 at 13:16

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.

share|improve this answer

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

share|improve this answer

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) {
        catch (InterruptedException ex) {
            // do something here
share|improve this answer
My question was unclear. Sorry about that. Out of curiosity, what was the intent behind using a join with the previous thread ? – James Poulson Mar 29 '13 at 16:21
It's showing a basic technique for sequencing threads. In this case, it does the exact opposite of what you want. However, once you have a linked list of threads (which is the other thing that it demonstrates), you can walk that list in any direction. – parsifal Mar 29 '13 at 16:27
Ah, this is why I love PSE so much. An anonymous downvote, followed by an upvote to a comment but no corresponding upvote to the answer. – parsifal Mar 29 '13 at 17:34
I downvoted you because you dumped code in an answer box and posted basically saying "come back if you have questions". Your comment getting upvoted, instead of your answer should be an obvious indication to you that your answer was insufficient. In a year or two from now, when you're nowhere to be found, such an answer will have much less value. Don't half-ass it and you'll get some upvotes. Likewise, as you've admitted, it does the opposite of what the question asks for. As it is, I may have put more effort into this comment than you did in the original answer. THIS is why I love PSE. – Steve Evers Mar 29 '13 at 20:03
@SteveEvers - I respect you far more now, after you've put the effort in to explain your downvote, than I did 4 hours ago. That may or may not matter to you. As for putting more effort into the comment than I put into my answer, I doubt it. I will explain more in the next comment, which might help anyone in the future. – parsifal Mar 29 '13 at 20:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.