多线程能否在Java中的直接映射的ByteBuffer上看到写入? [英] Can multiple threads see writes on a direct mapped ByteBuffer in Java?

查看:500
本文介绍了多线程能否在Java中的直接映射的ByteBuffer上看到写入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ByteBuffers 制作的内容内存映射文件(通过 FileChannel.map())以及内存中的直接ByteBuffers。我试图理解并发和内存模型约束。

I'm working on something that uses ByteBuffers built from memory-mapped files (via FileChannel.map()) as well as in-memory direct ByteBuffers. I am trying to understand the concurrency and memory model constraints.

我已经读过所有相关的Javadoc(和源)像FileChannel,ByteBuffer,MappedByteBuffer等。看起来很清楚,一个特定的ByteBuffer(和相关的子类)有一堆字段,并且状态不受内存模型的保护。因此,如果跨线程使用该缓冲区,则必须在修改特定ByteBuffer的状态时进行同步。常见的技巧包括使用ThreadLocal包装ByteBuffer,重复(同步时)以获得指向相同映射字节的新实例等。

I have read all of the relevant Javadoc (and source) for things like FileChannel, ByteBuffer, MappedByteBuffer, etc. It seems clear that a particular ByteBuffer (and relevant subclasses) has a bunch of fields and the state is not protected from a memory model point of view. So, you must synchronize when modifying state of a particular ByteBuffer if that buffer is used across threads. Common tricks include using a ThreadLocal to wrap the ByteBuffer, duplicate (while synchronized) to get a new instance pointing to the same mapped bytes, etc.

/ p>

Given this scenario:


  1. manager对于整个文件具有映射的字节缓冲区 B_all 2gb)

  2. 经理在B_all上调用duplicate(),position(),limit()和slice(),创建一个新的较小的ByteBuffer B_1 这个文件的一个块,并将它给线程T1

  3. manager做所有相同的东西创建一个ByteBuffer B_2 指向相同的映射字节并将其赋予线程T2

  1. manager has a mapped byte buffer B_all for the entire file (say it's <2gb)
  2. manager calls duplicate(), position(), limit(), and slice() on B_all to create a new smaller ByteBuffer B_1 that a chunk of the file and gives this to thread T1
  3. manager does all the same stuff to create a ByteBuffer B_2 pointing to the same mapped bytes and gives this to thread T2

我的问题是:T1可以写入B_1和T2同时写入B_2并保证看到对方的变化? T3可以使用B_all读取这些字节,并保证看到来自T1和T2的更改?

My question is: Can T1 write to B_1 and T2 write to B_2 concurrently and be guaranteed to see each other's changes? Could T3 use B_all to read those bytes and be guaranteed to see the changes from both T1 and T2?

我知道映射文件中的写入不一定被看到进程,除非您使用force()指示操作系统将页面写入磁盘。我不在乎。假设这个问题,这个JVM是写一个映射文件的唯一进程。

I am aware that writes in a mapped file are not necessarily seen across processes unless you use force() to instruct the OS to write the pages down to disk. I don't care about that. Assume for this question that this JVM is the only process writing a single mapped file.

注意:我不是在寻找猜测使自己相当不错)。我想参考一些确定的什么是(或不是)保证内存映射直接缓冲区。

Note: I am not looking for guesses (I can make those quite well myself). I would like references to something definitive about what is (or is not) guaranteed for memory-mapped direct buffers. Or if you have actual experiences or negative test cases, that could also serve as sufficient evidence.

更新:我已经做了一些测试,使用多个线程并行写入同一个文件,到目前为止,似乎这些写入立即从其他线程可见。

Update: I have done some tests with having multiple threads write to the same file in parallel and so far it seems those writes are immediately visible from other threads. I'm not sure if I can rely on that though.

推荐答案

使用JVM的内存映射只是一个简单的包装器CreateFileMapping (Windows)或mmap(posix)。因此,您可以直接访问操作系统的缓冲区缓存。这意味着这些缓冲区是操作系统认为文件包含(并且操作系统将最终同步文件以反映这些)。

Memory mapping with the JVM is just a thin wrapper around CreateFileMapping (Windows) or mmap (posix). As such, you have direct access to the buffer cache of the OS. This means that these buffers are what the OS considers the file to contain (and the OS will eventually synch the file to reflect this).

所以没有必要调用force()在进程之间进行同步。进程已经同步(通过操作系统 - 甚至读/写访问相同的页面)。强制操作系统和驱动器控制器之间的同步(在驱动器控制器和物理磁盘之间可能有一些延迟,但是您没有硬件支持来做任何事情)。

So there is no need to call force() to sync between processes. The processes are already synched (via the OS - even read/write accesses the same pages). Forcing just synchs between the OS and the drive controller (there can be some delay between the drive controller and the physical platters, but you don't have hardware support to do anything about that).

无论如何,内存映射文件是线程和/或进程之间共享内存的接受形式。这个共享内存和例如Windows中的虚拟内存的命名块之间的唯一区别是最终与磁盘同步(实际上,mmap通过映射/ dev / null而没有文件内容的虚拟内存)。

Regardless, memory mapped files are an accepted form of shared memory between threads and/or processes. The only difference between this shared memory and, say, a named block of virtual memory in Windows is the eventual synchronization to disk (in fact mmap does the virtual memory without a file thing by mapping /dev/null).

从多个进程/线程读取写入内存仍然需要一些同步,因为处理器能够执行无序执行(不确定有多少与JVM交互,但是你可以' t make presumptions),但是从一个线程写入一个字节将具有与正常写入堆中任何字节相同的保证。一旦你写了它,每个线程和每个进程,将看到更新(即使通过打开/读取操作)。

Reading writing memory from multiple processes/threads does still need some synch, as processors are able to do out-of-order execution (not sure how much this interacts with JVMs, but you can't make presumptions), but writing a byte from one thread will have the same guarantees as writing to any byte in the heap normally. Once you have written to it, every thread, and every process, will see the update (even through an open/read operation).

有关更多信息,查找mmap在posix(或CreateFileMapping for Windows,其建立方式几乎相同。)

For more info, look up mmap in posix (or CreateFileMapping for Windows, which was built almost the same way.

这篇关于多线程能否在Java中的直接映射的ByteBuffer上看到写入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆