Threads and file writing

I have a java program which uses 20 threads. Every one of them write their results in a file called output.txt.

I always get a different number of lines in output.txt.

Can it be a problem with the synchronization of threads? Is there a way to handle this?


Solution 1:

can it be a problem of synchronization of threads?

Yes.

There's a way to handle this?

Yes, ensure that writes are serialized by synchronizing on a relevant mutex. Or alternately, have only one thread that actually outputs to the file, and have all of the other threads simply queue text to be written to a queue that the one writing thread draws from. (That way the 20 main threads don't block on I/O.)

Re the mutex: For instance, if they're all using the same FileWriter instance (or whatever), which I'll refer to as fw, then they could use it as a mutex:

synchronized (fw) {
    fw.write(...);
}

If they're each using their own FileWriter or whatever, find something else they all share to be the mutex.

But again, having a thread doing the I/O on behalf of the others is probably also a good way to go.

Solution 2:

I'd suggest you to organize it this way: One thread-consumer will consume all data and write it to the file. All worker threads will produce data to the consumer thread in synchronous way. Or with multiple threads file writing you can use some mutex or locks implementations.