Java : ReentrantReadWriteLock with priority

前端 未结 2 611
轮回少年
轮回少年 2021-01-05 11:30

The following is the typical reader and writer pattern (a lot of reads and few writes)

  private ReadWriteLock lock = new ReentrantReadWriteLock();
  private         


        
相关标签:
2条回答
  • 2021-01-05 11:58

    According to the javadoc, the jdk implementation does not have any reader/writer priority. however, if you use the "fair" implementation, then the lock is granted in fifo order (still no reader/writer preference), so at least future readers will not block waiting writers.

    0 讨论(0)
  • 2021-01-05 12:12

    Write locks are actually already prioritized above reader locks.

    If a thread wants to read the resource, it is okay as long as no threads are writing to it, and no threads have requested write access to the resource. By up-prioritizing write-access requests we assume that write requests are more important than read-requests.

    please refer to this excellent post for more details.

    Edit: it is important to note that this holds for the fair mode only. thanks @jtahlborn!

    0 讨论(0)
提交回复
热议问题