javamultithreadinglockingdeadlockreentrantreadwritelock

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


What I know is:


Solution

  • readLock.lock();


    writeLock.lock();


    Combining these you can arrange for only one thread at a time to have write access, but as many readers as you like can read at the same time except when a thread is writing.

    Put another way. Every time you want to read from the structure, take a read lock. Every time you want to write, take a write lock. This way whenever a write happens no-one is reading (you can imagine you have exclusive access), but there can be many readers reading at the same time so long as no-one is writing.