在ReentrantReadWriteLock
文档中说:
writer can acquire the read lock, but not vice-versa
如果我理解正确,这意味着您可以从同一个线程执行:
//thread1
lock.writeLock().lock()
lock.readLock().lock()
print("this line executes")
这是有道理的:如果您已经锁定,则write
没有其他线程可以输入锁定的代码。但是如果你锁定了,如果没有其他线程锁定read
,为什么你不能进入同一个线程中的块?所以这不起作用:write
read
//thread1
lock.readLock().lock()
lock.writeLock().lock()
print("this line doesn't execute")
为什么必须先解锁read
才能锁定write
在同一个线程中?