Note that there are some explanatory texts on larger screens.

plurals
  1. POCan multiple threads see writes on a direct mapped ByteBuffer in Java?
    text
    copied!<p>I'm working on something that uses <a href="http://download.oracle.com/javase/6/docs/api/java/nio/ByteBuffer.html" rel="noreferrer">ByteBuffers</a> built from memory-mapped files (via <a href="http://download.oracle.com/javase/6/docs/api/java/nio/channels/FileChannel.html#map%28java.nio.channels.FileChannel.MapMode,%20long,%20long%29" rel="noreferrer">FileChannel.map()</a>) as well as in-memory direct ByteBuffers. I am trying to understand the concurrency and memory model constraints.</p> <p>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> <p>Given this scenario:</p> <ol> <li>manager has a mapped byte buffer <code>B_all</code> for the entire file (say it's &lt;2gb)</li> <li>manager calls duplicate(), position(), limit(), and slice() on B_all to create a new smaller ByteBuffer <code>B_1</code> that a chunk of the file and gives this to thread T1</li> <li>manager does all the same stuff to create a ByteBuffer <code>B_2</code> pointing to the same mapped bytes and gives this to thread T2</li> </ol> <p>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?</p> <p>I am aware that writes in a mapped file are not necessarily seen across <em>processes</em> 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.</p> <p><strong>Note:</strong> 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.</p> <p><strong>Update:</strong> 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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload