r/javahelp • u/feralalien • Oct 09 '15
Having trouble using the ReentrantReadWriteLock
I seem to be having trouble getting ReentrantReadWriteLock to work. I have done it as far as I can tell, correctly, but my audit method seems to be happening during the move method resulting in inconsistent values. From my understanding this should not be happening because a read lock should wait until the write lock has released. Any insight would be very appreciated!
5
Upvotes
View all comments
Show parent comment
2
u/[deleted] Oct 09 '15
I'm saying that if you want your calls to run in perfect sequence, like :
transaction
audit
transaction
audit
etc, then you have to hold the lock. If you release and reacquire then it may run in other sequences, e.g.
transaction
transaction
audit
transaction
transaction
audit
audit
audit
In the code you posted you still have no control over this detail:
Timer interrupts in the processor may occur at any time, even in the middle between those two calls. However, if you invert their order,
Then you're sure that no other transaction can start until the current one has been audited.
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html
If you don't care about the ordering of transactions and audits then, of course, you can ignore the whole thing.