I have a ByteBuffer in java and you want to read, then conditionally change this byte, for example. using a method, for example:
public void updateByte(int index) { byte b = this.buffer.getByte(index); if (b == someByteValue) { this.buffer.setByte(index, someNewByte); } }
How can I make sure that reading and then changing the byte happens atomically?
I donβt want to synchronize the whole ByteBuffer or updateByte , since I want several threads to be able to read / write different bytes of the buffer at the same time (i.e. updateByte can call many threads at the same time, while index is different).
The ByteBuffer I'm using is not supported by byte [], so bb.hasArray() == false in the above example.
java concurrency memory
Dave challis
source share