Multiple threads and single output source

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật