ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ReadHoldingWriteLock.java
Revision: 1.8
Committed: Mon Aug 10 03:13:33 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +0 -1 lines
Log Message:
delete unwanted blank lines

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/publicdomain/zero/1.0/
5 */
6 import java.util.concurrent.locks.*;
7
8 class ReadHoldingWriteLock {
9
10 public static void main(String[] args) throws Exception {
11 ReadHoldingWriteLock t = new ReadHoldingWriteLock();
12 t.testReadAfterWriteLock();
13 t.testReadHoldingWriteLock();
14 t.testReadHoldingWriteLock2();
15 t.testReadHoldingWriteLockFair();
16 t.testReadHoldingWriteLockFair2();
17 }
18
19 static final long SHORT_DELAY_MS = 50;
20 static final long MEDIUM_DELAY_MS = 200;
21
22 void assertTrue(boolean b) {
23 if (!b) throw new Error();
24 }
25
26 /**
27 * Readlocks succeed after a writing thread unlocks
28 */
29 public void testReadAfterWriteLock() throws Exception {
30 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
31 lock.writeLock().lock();
32 Thread t1 = new Thread(new Runnable() {
33 public void run() {
34 lock.readLock().lock();
35 lock.readLock().unlock();
36 }
37 });
38 Thread t2 = new Thread(new Runnable() {
39 public void run() {
40 lock.readLock().lock();
41 lock.readLock().unlock();
42 }
43 });
44
45 t1.start();
46 t2.start();
47 Thread.sleep(SHORT_DELAY_MS);
48 lock.writeLock().unlock();
49 t1.join(MEDIUM_DELAY_MS);
50 t2.join(MEDIUM_DELAY_MS);
51 assertTrue(!t1.isAlive());
52 assertTrue(!t2.isAlive());
53 }
54
55 /**
56 * Read trylock succeeds if write locked by current thread
57 */
58 public void testReadHoldingWriteLock()throws Exception {
59 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
60 lock.writeLock().lock();
61 assertTrue(lock.readLock().tryLock());
62 lock.readLock().unlock();
63 lock.writeLock().unlock();
64 }
65
66 /**
67 * Read lock succeeds if write locked by current thread even if
68 * other threads are waiting
69 */
70 public void testReadHoldingWriteLock2() throws Exception {
71 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
72 lock.writeLock().lock();
73 Thread t1 = new Thread(new Runnable() {
74 public void run() {
75 lock.readLock().lock();
76 lock.readLock().unlock();
77 }
78 });
79 Thread t2 = new Thread(new Runnable() {
80 public void run() {
81 lock.readLock().lock();
82 lock.readLock().unlock();
83 }
84 });
85
86 t1.start();
87 t2.start();
88 lock.readLock().lock();
89 lock.readLock().unlock();
90 Thread.sleep(SHORT_DELAY_MS);
91 lock.readLock().lock();
92 lock.readLock().unlock();
93 lock.writeLock().unlock();
94 t1.join(MEDIUM_DELAY_MS);
95 t2.join(MEDIUM_DELAY_MS);
96 assertTrue(!t1.isAlive());
97 assertTrue(!t2.isAlive());
98 }
99
100 /**
101 * Fair Read trylock succeeds if write locked by current thread
102 */
103 public void testReadHoldingWriteLockFair() throws Exception {
104 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
105 lock.writeLock().lock();
106 assertTrue(lock.readLock().tryLock());
107 lock.readLock().unlock();
108 lock.writeLock().unlock();
109 }
110
111 /**
112 * Fair Read lock succeeds if write locked by current thread even if
113 * other threads are waiting
114 */
115 public void testReadHoldingWriteLockFair2() throws Exception {
116 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
117 lock.writeLock().lock();
118 Thread t1 = new Thread(new Runnable() {
119 public void run() {
120 lock.readLock().lock();
121 lock.readLock().unlock();
122 }
123 });
124 Thread t2 = new Thread(new Runnable() {
125 public void run() {
126 lock.readLock().lock();
127 lock.readLock().unlock();
128 }
129 });
130
131 t1.start();
132 t2.start();
133 lock.readLock().lock();
134 lock.readLock().unlock();
135 Thread.sleep(SHORT_DELAY_MS);
136 lock.readLock().lock();
137 lock.readLock().unlock();
138 lock.writeLock().unlock();
139 t1.join(MEDIUM_DELAY_MS);
140 t2.join(MEDIUM_DELAY_MS);
141 assertTrue(!t1.isAlive());
142 assertTrue(!t2.isAlive());
143 }
144 }