Question

Is there any Lock in Java that would allow me to do this?

  • one WRITE operation at the time
  • infinite number of READ operations (even if there is a WRITE lock acquired)
  • optional but great: lock timeout - release lock after a while to avoid deadlock forever
Was it helpful?

Solution

It sounds like there is no circumstance under which reads cannot occur. Do you really need a read lock? If not, then you should just be able to use a Semaphore with 1 permit to protect the write permission. If you still want to track how many reads are happening without the ability to prevent any, you can always count how many reads are in flight with an AtomicInteger.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top