ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ReadHoldingWriteLock.java
Revision: 1.1
Committed: Mon May 2 19:19:38 2005 UTC (19 years ago) by dl
Branch: MAIN
Log Message:
Put misc performance tests into CVS

File Contents

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