KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.cache.tests;
8
9 import EDU.oswego.cs.dl.util.concurrent.ReadWriteLock;
10 import EDU.oswego.cs.dl.util.concurrent.ReentrantWriterPreferenceReadWriteLock;
11 import junit.framework.Test;
12 import junit.framework.TestCase;
13 import junit.framework.TestSuite;
14
15 /**
16  * Tests the various ReadWriteLock implementations
17  * @author Bela Ban
18  * @version $Id: ReadWriteLockTest.java,v 1.1.2.2 2005/04/06 21:07:03 starksm Exp $
19  */

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