I am trying to use MappedByteBuffer to allow concurrent reads in a file by multiple threads with the following restrictions:
- File too large to load into memory
- Themes should be able to read asynchronously (this is a web application)
- A file is never written by any stream.
- Each thread will always know the exact offset and length of the bytes that it needs to read (i.e., "no" search "by the application itself).
According to the docs ( https://docs.oracle.com/javase/8/docs/api/java/nio/Buffer.html ) Buffers are not thread safe because they retain their internal state (position, etc.). Is there a way to have simultaneous random access to a file without loading it all into memory?
Although FileChannel technically thread safe, from the docs:
If the channel of the channel is obtained from an existing stream or random access file, then the state of the channel of the file is closely related to the state of the object whose getChannel method returns the channel. Changing the position of the channel, either explicitly or by reading or writing bytes, will change the file position of the source object and vice versa
Thus, it would seem that it is simply synchronized. If I were in new RandomAccessFile().getChannel().map() in each stream [edit: each read], this does not affect the I / O overhead, and each of them should read what MappedByteBuffers should avoid ?
source share