简体   繁体   English

多个RandomAccessFile对象可以将数据写入同一个文件吗?

[英]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? 在我的代码中,每个线程通过各自的RandomAccessFile对象将数据写入同一文件。需要同步吗? Sorry for my poor English. 抱歉我的英语不好。

Can multiple RandomAccessFile objects write data to same file? 多个RandomAccessFile对象可以将数据写入同一个文件吗?

Yes, we do this in our libraries in Chronicle. 是的,我们在Chronicle的图书馆中这样做。

In my code, each thread writes data to the same file through respective RandomAccessFile object.Does that need to be synchronized? 在我的代码中,每个线程通过各自的RandomAccessFile对象将数据写入同一文件。需要同步吗?

You still need to worry about thread safety. 您仍然需要担心线程安全。 synchronized or Lock will work however, this won't work across JVMs. 但是, synchronizedLock将起作用,这不适用于JVM。 If you have multiple JVM you need either shared locks of using low level off heap thread safe operations. 如果您有多个JVM,则需要使用低级别脱机堆线程安全操作的共享锁。 (Which is what we do as it is the fastest option) (这是我们所做的,因为它是最快的选择)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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