javamultithreadingthread-safetymmapmappedbytebuffer

Java mmap MappedByteBuffer


Let’s say I’ve mapped a memory region [0, 1000] and now I have MappedByteBuffer.

Can I read and write to this buffer from multiple threads at the same time without locking, assuming that each thread accesses different part of the buffer for exp. T1 [0, 500), T2 [500, 1000]?

If the above is true, is it possible to determine whether it’s better to create one big buffer for multiple threads, or smaller buffer for each thread?


Solution

  • Detailed Intro:

    If you wanna learn how to answer those questions yourself, check their implementation source codes:

    Now it gets a bit more complicated:

    When you wanna allocate a MappedByteBuffer, you will get either a

    Instead of having to browse internet pages, you could also simply download the source code packages for your Java version and attach them in your IDE so you can see the code in development AND debug modes. A lot easier.

    Short (incomplete) answer:

    Neither of them does secure against multithreading.

    On a side note, here also lies an implementation failure creep in the Java implementation:

    This design flaw comes from the step-by-step development of those classes (they were no all planned and implemented at the same time), AND the topic of package visibility, resulting in inversion of dependency/hierarchy of the implementation.

    Now to the true answer:

    If you wanna do proper object-oriented programming, you should NOT share resource unless utterly needed. This ESPECIALLY means that each Thread should have its very own Buffer.

    Advantage of having one global buffer: the only "advantage" is to reduce the additional memory consumption for additional object references. But this impact is SO MINIMAL (not even 1:10000 change in your app RAM consumption) that you will NEVER notice it. There's so many other objects allocated for any number of weird (Java) reasons everywhere that this is the least of your concerns. Plus you would have to introduce additional data (index boundaries) which lessens the 'advantage' even more.

    The big Advantages of having separate buffers:

    Conclusion:

    If you wanna have a really bad design choice - which WILL make life a lot harder later on - you go with one global Buffer.

    If you wanna do it the proper OO way, separate those buffers. No convoluted dependencies and side effect problems.