Search code examples
javamultithreadingbuffernettynio

Parallel/Multithreaded reading from ByteBuffer/Netty ByteBuff


Heyho,

ByteBuffers as well as netty's ByteBuff use indices to store where they currently "are". At the start of my application I load multiple files in ByteBuffers/ByteBuffs to read later from those. Also the bytebuffers are immutable after loading. My problem is now that multiple clients should be able to read from those byte buffers, but because they use the same reader/writer indices it won't work. Is there a easy way of maintaining the indices maybe per thread? Does netty have some "tools" to achive this?

I've already read that nio ByteBuffers do not support multiple threads, but is this also true if you only read from them?

Basically I'm only looking for a way to send data, which is stored in memory, very fast over netty.


Solution

  • You can call duplicate on the ByteBuf for every new Thread and use the returned ByteBuf. Those will share the same content but not the same indices.

    ByteBuf duplicate = ByteBuf.duplicate();