If you want to keep order (i.e. message 1 in the output stream got to message 2), you must block the stream. This in turn reduces concurrency. (All threads will be put on the lock / semaphore queue and wait for the thread to become available to them)
If you are only interested in writing a stream at the same time and do not care about the order, you can have buffers for each stream. Each stream is written to its own buffer. When the buffer is full, it receives a lock (which may include waiting for a lock) in the stream and empties its contents into the stream.
Edit: I just realized that if you care about the order and still want multithreading, if you also write time in the output stream in unix format (as long). After the stream is dumped onto some other container, the contents can be sorted by time, and you should have an ordered file.
Adrian
source share