ReadLock can be held by multiple threads and exclude any WriteLock while in effect, while WriteLock is completely mutually exclusive.
Conversely, it is impossible to upgrade ReadLock to WriteLock. At this time, the thread will keep the Readlock holding. The sequence is: first obtain WriteLock, then obtain ReadLock, and then release WriteLock. WriteLock can be downgraded to ReadLock.In terms of reentrancy (introduced in ReentrantLock above), its internal WriteLock can obtain ReadLock, but on the contrary, if ReadLock wants to obtain WriteLock, never think about it.Then is to summarize the characteristics of this lock mechanism: It and the latter are separate realizations, and there is no inheritance or realization relationship between each other. To ReentrantReadWriteLock, the first thing to do is to draw a clear line with ReentrantLock. The prerequisite for the thread to enter the write lock: No write request or write request, but the calling thread and the thread holding the lock are the same one The precondition for the thread to enter the read lock: ReentrantReadWriteLock will use two locks to solve the problem, a read lock and a write lock. They are handled in different ways.įirst of all, we only consider the case of fair competition, to see what Java methods can achieve the reader writer problem. The synchronization problem of Reader and Writer is divided into three situations: reader first, weak writer first (fair competition), and strong writer first. (3) Multiple writers are not allowed to operate at the same time. (2) Readers and writers are not allowed to operate at the same time (1) Allow multiple readers to perform read operations at the same time As far as shared data is concerned, Reader and Writer are two sets of concurrent processes sharing a set of data areas, requiring: Data (files, records) in a computer system are often shared by multiple processes, but some of these processes may only require data to be read (called Reader) other processes require data to be modified (called Writer). The reader-writer problem (Readers-Writers problem) is also a classic concurrent programming problem, a synchronization problem that often occurs.