Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 940
RazorMx Avatar asked Apr 02 '12 07:04

RazorMx


People also ask

Is writing to a file thread-safe?

You can write to file in a thread-safe manner using a mutex lock via the threading. Lock class.

Can two threads write to the same file?

You can have multiple threads write to the same file - but one at a time. All threads will need to enter a synchronized block before writing to the file. In the P2P example - one way to implement it is to find the size of the file and create a empty file of that size.

What is thread example?

Thread is often referred to as a lightweight process. The process can be split down into so many threads. For example, in a browser, many tabs can be viewed as threads. MS Word uses many threads - formatting text from one thread, processing input from another thread, etc.

Can multiple threads write to the same file Java?

You have to append to the existing file ( FileOutputStream has a boolean flag for that). But you would also have to make the writing synchronized , to make sure only one Thread writes at the same time. Don't use StringBuffer if you can use StringBuilder. In this case processId() can return a String.


1 Answers

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.

like image 71
T.J. Crowder Avatar answered Oct 12 '22 14:10

T.J. Crowder