简体   繁体   English

最好的并发锁:一次写入和无限次读取

[英]The best Lock for concurrency: one WRITE and infinite number of READ

Is there any Lock in Java that would allow me to do this? Java中是否有任何锁可以让我做到这一点?

  • one WRITE operation at the time 当时一次写操作
  • infinite number of READ operations (even if there is a WRITE lock acquired) 无限数量的READ操作(即使获得WRITE锁)
  • optional but great: lock timeout - release lock after a while to avoid deadlock forever 可选,但很棒:锁定超时-一段时间后释放锁定,以避免永远死锁

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. 如果没有,那么您应该只能使用具有1个许可的信号量来保护写许可权。 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 . 如果您仍想跟踪发生了多少次读取而又无法阻止任何读取,则可以始终使用AtomicInteger计数正在运行的读取数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM