Reputation: 43
public class WriteThread extends Thread{
@Override
public void run() {
RandomAccessFile randomAccessFile = new RandomAccessFile(fileName, "rwd");
randomAccessFile.seek(threadPosition);
byte[] buffer = new byte[1024 * 8];
randomAccessFile.write(buffer, 0, threadLength);
}
}
In my code, each thread writes data to the same file through respective RandomAccessFile object.Does that need to be synchronized? Sorry for my poor English.
Upvotes: 4
Views: 1338
Reputation: 533500
Can multiple RandomAccessFile objects write data to same file?
Yes, we do this in our libraries in Chronicle.
In my code, each thread writes data to the same file through respective RandomAccessFile object.Does that need to be synchronized?
You still need to worry about thread safety. synchronized
or Lock
will work however, this won't work across JVMs. If you have multiple JVM you need either shared locks of using low level off heap thread safe operations. (Which is what we do as it is the fastest option)
Upvotes: 3