1.1.1.
three write lock downgrade for read-write lock application
Reentrantreadwritelock also features write-lock demotion, which has some relationship to reentrant sex.
(1) you can downgrade to read lock when holding a write lock.
(2) You cannot upgrade to a write lock while holding a read lock.
Reentrantreadwritelock and reentrantlock are similar in that they have a feature that can be re-entered. Reentrant means that the thread that has acquired the lock can acquire the lock again, ensuring that the lock and unlock are the same number of times.
Packagecom.test.concurrence;ImportJava.util.Random;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;ImportJava.util.concurrent.locks.ReadWriteLock;ImportJava.util.concurrent.locks.ReentrantReadWriteLock; classMySharedResource3 {//Constructor: Fair lock. PrivateReadwritelock lock =NewReentrantreadwritelock (true); Public voidReadintTaskID) {Lock.readlock (). Lock ();Try{ for(inti=0;i<10;i++) {x--; System.out.println (TaskID+ "," + i + ": Read called:x:" + x + "," +Thread.CurrentThread (). GetName ()); Thread.Sleep (100);}} Catch(interruptedexception e) {e.printstacktrace ();}finally{lock.readlock (). Unlock ();}} Public voidDowngrade (intTaskID) {//Hold write lockLock.writelock (). Lock (); System.out.println (TaskID+ "Writelock locked."); Try{ for(inti=0;i<10;i++) {x++; System.out.println (TaskID+ "," + i + ": Write called:x:" + x + "," +Thread.CurrentThread (). GetName ()); Thread.Sleep (100);} //continue to hold the read lock in the case of a write lock already held. Lock.readlock (). Lock (); System.out.println (TaskID+ "Writelock-readlock locked."); //after the write lock is released, it is downgraded to read lock. lock.writelock (). Unlock (); System.out.println (TaskID+ "Writelock unlocked."); for(inti=0;i<10;i++) {x++; System.out.println (TaskID+ "," + i + ": Read in Write:x:" + x + "," +Thread.CurrentThread (). GetName ()); Thread.Sleep (1000);} //release the read lock. lock.readlock (). Unlock (); System.out.println (TaskID+ "Readlock locked."); } Catch(interruptedexception e) {e.printstacktrace ();}finally{//Release the lock. Lock.readlock (). Lock (); Lock.writelock (). Unlock ();}} Private intx = 0;} Public classReadWriteLockTest3 { Public Static voidMain (string[] args) {FinalMySharedResource3 sr =NewMySharedResource3 (); Executorservice Executorservice=Executors.newcachedthreadpool ();inti; for(i=0;i<10;i++) {Final intTaskID =I;executorservice.execute (NewRunnable () {@Override Public voidrun () {System.out.println ("TaskID:" +taskid); while(true){if(0 = = (taskid% 2) {sr.downgrade (taskid);}Else{sr.read (TaskID);}}});} Executorservice.shutdown ();}}
observing the results of the operation, you can see that in the downgrade () function, when a thread executes a block of code after the write lock has been downgraded to a read lock, the other thread can obtain a read lock.
One possible result of the operation is as follows:
taskid:0
Taskid:4
Taskid:3
Taskid:1
Taskid:2
0 Writelock locked.
Taskid:5
0,0:write Called:x:1, Pool-1-thread-1
Taskid:6
Taskid:9
Taskid:8
Taskid:7
0,1:write Called:x:2, Pool-1-thread-1
0,2:write Called:x:3, Pool-1-thread-1
0,3:write Called:x:4, Pool-1-thread-1
0,4:write Called:x:5, Pool-1-thread-1
0,5:write Called:x:6, Pool-1-thread-1
0,6:write Called:x:7, Pool-1-thread-1
0,7:write Called:x:8, Pool-1-thread-1
0,8:write Called:x:9, Pool-1-thread-1
0,9:write Called:x:10, Pool-1-thread-1
0 writelock, Readlock locked.
0 Writelock Unlocked.
0,0:read in Write:x:11, pool-1-thread-1
3,0:read Called:x:10, Pool-1-thread-4
3,1:read Called:x:9, Pool-1-thread-4
3,2:read Called:x:8, Pool-1-thread-4
3,3:read Called:x:7, Pool-1-thread-4
3,4:read Called:x:6, Pool-1-thread-4
3,5:read Called:x:5, Pool-1-thread-4
3,6:read Called:x:4, Pool-1-thread-4
3,7:read Called:x:3, Pool-1-thread-4
3,8:read Called:x:2, Pool-1-thread-4
3,9:read Called:x:1, Pool-1-thread-4
0,1:read in Write:x:2, pool-1-thread-1
0,2:read in Write:x:3, pool-1-thread-1
0,3:read in Write:x:4, pool-1-thread-1
0,4:read in Write:x:5, pool-1-thread-1
0,5:read in Write:x:6, pool-1-thread-1
0,6:read in Write:x:7, pool-1-thread-1
0,7:read in Write:x:8, pool-1-thread-1
0,8:read in Write:x:9, pool-1-thread-1
0,9:read in Write:x:10, pool-1-thread-1
0 Readlock locked.
4 Writelock locked.
4,0:write Called:x:11, pool-1-thread-5
4,1:write Called:x:12, pool-1-thread-5
4,2:write Called:x:13, pool-1-thread-5
4,3:write called:x:14, pool-1-thread-5
4,4:write called:x:15, pool-1-thread-5
4,5:write Called:x:16, pool-1-thread-5
4,6:write Called:x:17, pool-1-thread-5
4,7:write called:x:18, pool-1-thread-5
4,8:write called:x:19, pool-1-thread-5
4,9:write called:x:20, pool-1-thread-5
4 Writelock, Readlock locked.
4 Writelock Unlocked.
1,0:read called:x:19, Pool-1-thread-2
4,0:read in Write:x:20, pool-1-thread-5
1,1:read called:x:19, Pool-1-thread-2
1,2:read called:x:18, Pool-1-thread-2
1,3:read Called:x:17, Pool-1-thread-2
1,4:read Called:x:16, Pool-1-thread-2
1,5:read called:x:15, Pool-1-thread-2
1,6:read called:x:14, Pool-1-thread-2
1,7:read Called:x:13, Pool-1-thread-2
1,8:read Called:x:12, Pool-1-thread-2
1,9:read Called:x:11, Pool-1-thread-2
4,1:read in Write:x:12, pool-1-thread-5
4,2:read in Write:x:13, pool-1-thread-5
4,3:read in Write:x:14, pool-1-thread-5
4,4:read in Write:x:15, pool-1-thread-5
4,5:read in Write:x:16, pool-1-thread-5
4,6:read in Write:x:17, pool-1-thread-5
4,7:read in Write:x:18, pool-1-thread-5
4,8:read in Write:x:19, pool-1-thread-5
4,9:read in Write:x:20, pool-1-thread-5
4 Readlock locked.
2 writelock locked.
2,0:write called:x:21, pool-1-thread-3
2,1:write called:x:22, pool-1-thread-3
2,2:write called:x:23, pool-1-thread-3
2,3:write called:x:24, pool-1-thread-3
2,4:write called:x:25, pool-1-thread-3
2,5:write called:x:26, pool-1-thread-3
2,6:write called:x:27, pool-1-thread-3
2,7:write called:x:28, pool-1-thread-3
2,8:write called:x:29, pool-1-thread-3
2,9:write called:x:30, pool-1-thread-3
2 Writelock, Readlock locked.
2 Writelock Unlocked.
5,0:read called:x:29, pool-1-thread-6
2,0:read in Write:x:30, pool-1-thread-3
5,1:read called:x:29, pool-1-thread-6
5,2:read called:x:28, pool-1-thread-6
5,3:read called:x:27, pool-1-thread-6
5,4:read called:x:26, pool-1-thread-6
5,5:read called:x:25, pool-1-thread-6
5,6:read called:x:24, pool-1-thread-6
5,7:read called:x:23, pool-1-thread-6
5,8:read called:x:22, pool-1-thread-6
5,9:read called:x:21, pool-1-thread-6
2,1:read in Write:x:22, pool-1-thread-3
2,2:read in Write:x:23, pool-1-thread-3
2,3:read in Write:x:24, pool-1-thread-3
2,4:read in Write:x:25, pool-1-thread-3
2,5:read in Write:x:26, pool-1-thread-3
2,6:read in Write:x:27, pool-1-thread-3
2,7:read in Write:x:28, pool-1-thread-3
2,8:read in write:x:29, pool-1-thread-3
2,9:read in Write:x:30, pool-1-thread-3
2 readlock locked.
6 Writelock locked.
6,0:write called:x:31, pool-1-thread-7
6,1:write called:x:32, pool-1-thread-7
6,2:write called:x:33, pool-1-thread-7
6,3:write called:x:34, pool-1-thread-7
6,4:write called:x:35, pool-1-thread-7
6,5:write called:x:36, pool-1-thread-7
6,6:write called:x:37, pool-1-thread-7
6,7:write called:x:38, pool-1-thread-7
6,8:write called:x:39, pool-1-thread-7
6,9:write called:x:40, pool-1-thread-7
6 Writelock, Readlock locked.
6 Writelock Unlocked.
6,0:read in write:x:41, pool-1-thread-7
9,0:read called:x:40, pool-1-thread-10
9,1:read called:x:39, pool-1-thread-10
9,2:read called:x:38, pool-1-thread-10
9,3:read called:x:37, pool-1-thread-10
9,4:read called:x:36, pool-1-thread-10
9,5:read called:x:35, pool-1-thread-10
9,6:read called:x:34, pool-1-thread-10
9,7:read called:x:33, pool-1-thread-10
9,8:read called:x:32, pool-1-thread-10
9,9:read called:x:31, pool-1-thread-10
6,1:read in Write:x:32, pool-1-thread-7
6,2:read in Write:x:33, pool-1-thread-7
6,3:read in Write:x:34, pool-1-thread-7
6,4:read in Write:x:35, pool-1-thread-7
6,5:read in write:x:36, pool-1-thread-7
The API Doc is about reentrant and degraded.
Reentrancy
This lock allows both readers and writers to reacquire read or write locks in the style of a reentrantlock. Non-reentrant readers is not allowed until all write locks held by the writing thread has been released.
Additionally, a writer can acquire the read lock, but not vice-versa. Among other applications, reentrancy can is useful when write locks is held during calls or callbacks to methods that per Form reads under read locks. If a reader tries to acquire the write lock it'll never succeed.
Chinese
Re-entry Accessibility
this lock (Reentrantreadwritelock) allow read threads and write threads to follow Reentrantlock the model to get again to read or write the lock. Non-reentrant reads are not allowed until all write locks that have been held by the write thread are freed.
In addition, the write thread is able to acquire a read lock, but not in turn. In other applications, when a read lock is required in a call or callback method, it is useful to have a write lock in case it is already held. If a read thread attempts to acquire a write lock, it will not succeed.
Lock downgrading
Reentrancy also allows downgrading from the write lock to a read lock, by acquiring the write lock, then the read lock and Then releasing the write lock. However, upgrading from a read lock to the write lock are not possible.
Chinese
Lock downgrade
By acquiring a write lock, acquiring a read lock, and then releasing the write lock, Reentrant also allows the downgrade from a write lock to a read lock. However, it is not possible to upgrade from a read lock to a write lock.
Java concurrency Programming (15) Concurrent Lock read/write Lock (cont.) Write lock downgrade