java - Write lock degradation problem in read-write lock
大家讲道理
大家讲道理 2017-06-12 09:24:17
0
3
1217

When the write lock is downgraded, the thread that has currently acquired the write lock is required to hold the write lock, then acquire the read lock, and then release the write lock.

So why is it required to hold the write lock? ?

The above picture mentions "Then the current thread cannot sense the data update of thread T". How to understand this. If thread T acquires the write lock and then updates the data, the current thread cannot perceive the data update before the write lock is released. This is no problem, but once thread T releases the write lock, the current thread will You can feel the update of the data. It should be no problem to understand here, right?

help me to explain,thx

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(3)
習慣沉默

This is explaining the lock downgrade. First, the write lock is exclusive, the read lock is shared, and then the read and write locks are mutually exclusive between threads. The premise of lock downgrade is that all threads want to be sensitive to data changes, but because of the write There is only one lock, so degradation will occur.
If you release the write lock first and then acquire the read lock, other threads may acquire the write lock before acquiring it, blocking the acquisition of the read lock, and you will not be able to detect data changes. Therefore, you need to hold the write lock first to ensure that the data does not change, acquire the read lock, and then release the write lock.

伊谢尔伦

Write locks do not allow other processes to read and write operations, while read locks allow read operations.
Based on the example you captured, when you release the write lock, the T process acquires the write lock. At this time, you cannot acquire the read lock, so you must acquire the read lock first and then release the write lock.

First of all, you don’t understand the meaning of read-write lock. The existence of read lock means that other write operations are not allowed.
According to the example you provided, there may be a transaction thread that does not want its operation to be interrupted by other threads, and this transaction operation may be divided into multiple parts to update different data (or tables) or even be very time-consuming. If the write lock is used exclusively for a long time, it is obviously not allowed for some high-response applications, so after completing some write operations, the read lock is used to downgrade to allow response to the read operations of other processes. The lock is only released when all transactions are completed.
According to your understanding, if the write lock is occupied by other threads, then the transaction thread will have to interrupt and wait for other write locks to be released.

我想大声告诉你

"Then the current thread cannot perceive the data update of thread T"

When T1 reads data, the data value is copied to the context of the current thread, so other threads have no way to sense whether the data has been updated. As long as the data is submitted to the memory, that is, the Heap, other threads can get the latest value.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template