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?
You can write to file in a thread-safe manner using a mutex lock via the threading. Lock class.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With