Java ReentrantReadWriteLock 请求
只是一个关于 Java 中的 ReadWriteLocks(特别是 ReentrantReadWriteLock 实现)的简单问题,因为我没有找到清晰的 sun 文档。
如果一个线程持有读锁,而另一个线程请求写锁,会发生什么情况? 写锁线程是否必须等待所有当前持有的读锁被释放? 另外,在授予并释放写锁之前,是否所有新的读锁请求都会被阻止?
谢谢
Just a quick question about ReadWriteLocks in Java (specifically the ReentrantReadWriteLock implementation) as I don’t find the sun documentation clear.
What happens if a read lock is held by a thread when a write lock is requested by another?
Does the write lock thread have to wait for all currently held read-locks to be released?
Also, do all new read-lock requests get blocked until the write-lock is granted and released?
Thanks
Javadoc 对此进行了解释:
是的,作者还得再等等。 但它只会等待当前持有的读锁。 任何后来到达的读者都会在作者之后排队。
是的
The Javadoc explains this:
Yes, the writer will have to wait. But it will only wait for the currently held read locks. Any readers arriving later will queue after the writer.
Yes