ReentrantReadWriteLock: what's the difference between ReadLock and WriteLock?

前端 未结 5 632
庸人自扰
庸人自扰 2020-11-30 20:33

What I know is:

  • ReadLock and WriteLock affect each other somehow
  • WriteLock is just like synchronized
  • ReadLock
5条回答
  •  爱一瞬间的悲伤
    2020-11-30 21:25

    Consider a situation: In a case when data structures are read-mostly - they are mutable and are sometimes modified, but most accesses involve mostly reading, so in these case we can relax the locking mechanism in away that we can allow multiple readers to access the data structures instead of readers waiting while one reader has released the lock. As long as each thread is guaranteed an up to date view of the shared data and no thread modifies it while the readers are viewing it, there will no problems. This is what read write allows : a resource can be accessed by multiple readers or a single writer at a time, but not both.

提交回复
热议问题