KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > lock > ReadWriteLockTest


1 package org.jboss.cache.tests.lock;
2
3 import EDU.oswego.cs.dl.util.concurrent.ReadWriteLock;
4 import EDU.oswego.cs.dl.util.concurrent.ReentrantWriterPreferenceReadWriteLock;
5 import junit.framework.Test;
6 import junit.framework.TestCase;
7 import junit.framework.TestSuite;
8
9 /**
10  * Tests the various ReadWriteLock implementations
11  * @author Bela Ban
12  * @version $Id: ReadWriteLockTest.java,v 1.1.1.1 2005/03/31 10:15:10 belaban Exp $
13  */

14 public class ReadWriteLockTest extends TestCase {
15    ReadWriteLock lock;
16    Exception JavaDoc ex=null;
17
18
19    protected void setUp() throws Exception JavaDoc {
20       super.setUp();
21       ex=null;
22    }
23
24    protected void tearDown() throws Exception JavaDoc {
25       super.tearDown();
26       lock=null;
27       if(ex != null)
28          throw ex;
29    }
30
31
32    public void testMoreWriteReleasesThanAcquisitions() throws InterruptedException JavaDoc {
33       lock=new ReentrantWriterPreferenceReadWriteLock();
34       lock.writeLock().acquire();
35       lock.writeLock().release();
36       lock.writeLock().release();
37    }
38
39    public void testMoreReadReleasesThanAcquisitions() throws InterruptedException JavaDoc {
40       lock=new ReentrantWriterPreferenceReadWriteLock();
41       lock.readLock().acquire();
42       lock.readLock().release();
43       try {
44          lock.readLock().release();
45          fail("read locks cannot be released more than acquired");
46       }
47       catch(IllegalStateException JavaDoc illegalStateEx) {
48
49       }
50    }
51
52    public void testSimple() throws InterruptedException JavaDoc {
53       lock=new ReentrantWriterPreferenceReadWriteLock();
54       lock.readLock().acquire();
55       lock.readLock().acquire();
56       lock.writeLock().acquire();
57       lock.writeLock().acquire();
58    }
59
60
61    public void testOneWriterMultipleReaders() throws InterruptedException JavaDoc {
62       lock=new ReentrantWriterPreferenceReadWriteLock();
63
64       Writer writer=new Writer("writer");
65       Reader reader1=new Reader("reader1");
66       Reader reader2=new Reader("reader2");
67
68       writer.start();
69       reader1.start();
70       reader2.start();
71
72       writer.join();
73       reader1.join();
74       reader2.join();
75    }
76
77    class Writer extends Thread JavaDoc {
78
79       public Writer(String JavaDoc name) {
80          super(name);
81       }
82
83       public void run() {
84          try {
85             log("acquiring WL");
86             lock.writeLock().acquire();
87             log("acquired WL successfully");
88             sleep(1000);
89          }
90          catch(InterruptedException JavaDoc e) {
91             ex=e;
92          }
93          finally {
94             log("releasing WL");
95             lock.writeLock().release();
96          }
97       }
98    }
99
100
101    class Reader extends Thread JavaDoc {
102
103       public Reader(String JavaDoc name) {
104          super(name);
105       }
106
107
108       public void run() {
109          try {
110             log("acquiring RL");
111             lock.readLock().acquire();
112             log("acquired RL successfully");
113             sleep(500);
114          }
115          catch(InterruptedException JavaDoc e) {
116             ex=e;
117          }
118          finally {
119             log("releasing RL");
120             lock.readLock().release();
121          }
122       }
123    }
124
125
126    static void sleep(long timeout) {
127       try {
128          Thread.sleep(timeout);
129       }
130       catch(InterruptedException JavaDoc e) {
131       }
132    }
133
134    static void log(String JavaDoc msg) {
135       System.out.println(Thread.currentThread().getName() + ": " + msg);
136    }
137
138    public static Test suite() {
139       return new TestSuite(ReadWriteLockTest.class);
140    }
141
142    public static void main(String JavaDoc[] args) {
143       junit.textui.TestRunner.run(suite());
144    }
145
146 }
147
Popular Tags