java nio 内存映射_Java NIO – 内存映射文件

我最近遇到了这个

article,它为内存映射文件提供了一个很好的介绍,以及它如何在两个进程之间共享.以下是读入文件的进程的代码:

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.RandomAccessFile;

import java.nio.MappedByteBuffer;

import java.nio.channels.FileChannel;

public class MemoryMapReader {

/**

* @param args

* @throws IOException

* @throws FileNotFoundException

* @throws InterruptedException

*/

public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException {

FileChannel fc = new RandomAccessFile(new File("c:/tmp/mapped.txt"), "rw").getChannel();

long bufferSize=8*1000;

MappedByteBuffer mem = fc.map(FileChannel.MapMode.READ_ONLY, 0, bufferSize);

long oldSize=fc.size();

long currentPos = 0;

long xx=currentPos;

long startTime = System.currentTimeMillis();

long lastValue=-1;

for(;;)

{

while(mem.hasRemaining())

{

lastValue=mem.getLong();

currentPos +=8;

}

if(currentPos < oldSize)

{

xx = xx + mem.position();

mem = fc.map(FileChannel.MapMode.READ_ONLY,xx, bufferSize);

continue;

}

else

{

long end = System.currentTimeMillis();

long tot = end-startTime;

System.out.println(String.format("Last Value Read %s , Time(ms) %s ",lastValue, tot));

System.out.println("Waiting for message");

while(true)

{

long newSize=fc.size();

if(newSize>oldSize)

{

oldSize = newSize;

xx = xx + mem.position();

mem = fc.map(FileChannel.MapMode.READ_ONLY,xx , oldSize-xx);

System.out.println("Got some data");

break;

}

}

}

}

}

}

但是,我对这种方法提出了一些意见/问题:

如果我们只在空文件上执行阅读器,即运行

long bufferSize=8*1000;

MappedByteBuffer mem = fc.map(FileChannel.MapMode.READ_ONLY, 0, bufferSize);

long oldSize=fc.size();

这将分配8000个字节,现在将扩展该文件.返回的缓冲区的限制为8000,位置为0,因此,读者可以继续读取空数据.发生这种情况后,读者将停止,因为currentPos == oldSize.

据说现在编写进来(代码被省略,因为大部分内容都很简单,可以从网站上引用) – 它使用相同的缓冲区大小,因此它将首先写入8000个字节,然后分配另外8000个,扩展文件.现在,如果我们假设这个过程暂停,然后我们回到阅读器,那么读者会看到文件的新大小并分配剩余部分(从位置8000到1600)并再次开始阅读,阅读另一个垃圾…

我是否有同步这两个操作的原因有点困惑.据我所知,任何对map的调用都可能会扩展文件,实际上是一个空缓冲区(用零填充)或者编写器可能刚刚扩展了文件,但还没有写入任何东西……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值