Java ReentrantReadWriteLock Example
GIAN Tutorials
8:26 AM
ReadWriteLock has two locks, one for read and one for write. Request for read lock from multiple threads can be acquired if there is no write request. If there is a write request, no other thread will be able to acquire read or write lock on that resource. ReentrantReadWriteLock example: It is an implementation of ReadWriteLock interface. It has two kinds of locks i.e. ReaderLock and WriterLock. Let’s understand it with the help of an example: We will have three runnable interfaces. Reader, WriterEven and WriterOdd. Reader: It will read number variable. WriterEven: It will write even digit to the number. WriterOdd: It will write odd digit to the number. We are going to use ReentrantReadWriteLock with fairness as true. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 ...