简体   繁体   中英

Can multiple RandomAccessFile objects write data to same file?

 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM