Reputation: 323
I am trying to implement read/write access (ONE read thread and ONE write thread) to a memory-mapped file in Java. I know that a FileChannel supports concurrency so I was wondering if something similar can be done with a memory-mapped file in Java. I can guarantee that I will NEVER be reading and writing to the same area of the MemoryMappedBuffer at the same time, in other words, I will be concurrently reading in one area with the read thread and writing to a different area with the write thread. Any luck?
Upvotes: 1
Views: 454
Reputation: 298133
If your threads are accessing different memory regions, there will never be any issues. Care must be taken regarding the position, limit and mark of a Buffer
, these value must not get accessed concurrently. If you restrain your code to only use the index-based accessor methods, you have no problem. Alternatively, you may use duplicate
to create one buffer per thread to have independent, thread-local positions and limits.
You can also use combinations of position
, limit
and slice
to create buffers which are enforced to cover different regions of the original buffer. They will also have independent positions and limits, therefore, they will never have any race condition once you have ensured that they don’t overlap and each buffer is confined to exactly one thread.
Upvotes: 1