ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LockSupportTest.java
Revision: 1.5
Committed: Sat Dec 27 19:26:43 2003 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.4: +5 -4 lines
Log Message:
Headers reference Creative Commons

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.5 * 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/licenses/publicdomain
5     * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9     import junit.framework.*;
10     import java.util.*;
11     import java.util.concurrent.*;
12     import java.util.concurrent.locks.*;
13    
14 dl 1.2 public class LockSupportTest extends JSR166TestCase{
15 dl 1.1 public static void main(String[] args) {
16     junit.textui.TestRunner.run (suite());
17     }
18     public static Test suite() {
19     return new TestSuite(LockSupportTest.class);
20     }
21    
22 dl 1.3 /**
23 dl 1.4 * park is released by unpark occuring after park
24 dl 1.3 */
25 dl 1.4 public void testPark() {
26 dl 1.3 Thread t = new Thread(new Runnable() {
27     public void run() {
28     try {
29 dl 1.1 LockSupport.park();
30 dl 1.3 } catch(Exception e){
31     threadUnexpectedException();
32 dl 1.1 }
33     }
34     });
35 dl 1.3 try {
36 dl 1.4 t.start();
37     Thread.sleep(SHORT_DELAY_MS);
38 dl 1.1 LockSupport.unpark(t);
39     t.join();
40     }
41     catch(Exception e) {
42 dl 1.3 unexpectedException();
43 dl 1.1 }
44     }
45    
46 dl 1.3 /**
47 dl 1.4 * park is released by unpark occuring before park
48     */
49     public void testPark2() {
50     Thread t = new Thread(new Runnable() {
51     public void run() {
52     try {
53     Thread.sleep(SHORT_DELAY_MS);
54     LockSupport.park();
55     } catch(Exception e){
56     threadUnexpectedException();
57     }
58     }
59     });
60     try {
61     t.start();
62     LockSupport.unpark(t);
63     t.join();
64     }
65     catch(Exception e) {
66     unexpectedException();
67     }
68     }
69    
70     /**
71     * park is released by interrupt
72     */
73     public void testPark3() {
74     Thread t = new Thread(new Runnable() {
75     public void run() {
76     try {
77     LockSupport.park();
78     threadAssertTrue(Thread.interrupted());
79     } catch(Exception e){
80     threadUnexpectedException();
81     }
82     }
83     });
84     try {
85     t.start();
86     Thread.sleep(SHORT_DELAY_MS);
87     t.interrupt();
88     t.join();
89     }
90     catch(Exception e) {
91     unexpectedException();
92     }
93     }
94    
95     /**
96     * park returns if interrupted before park
97     */
98     public void testPark4() {
99     final ReentrantLock lock = new ReentrantLock();
100     lock.lock();
101     Thread t = new Thread(new Runnable() {
102     public void run() {
103     try {
104     lock.lock();
105     LockSupport.park();
106     } catch(Exception e){
107     threadUnexpectedException();
108     }
109     }
110     });
111     try {
112     t.start();
113     t.interrupt();
114     lock.unlock();
115     t.join();
116     }
117     catch(Exception e) {
118     unexpectedException();
119     }
120     }
121    
122     /**
123     * parkNanos times out if not unparked
124 dl 1.3 */
125 dl 1.1 public void testParkNanos() {
126 dl 1.3 Thread t = new Thread(new Runnable() {
127     public void run() {
128     try {
129 dl 1.1 LockSupport.parkNanos(1000);
130 dl 1.3 } catch(Exception e){
131     threadUnexpectedException();
132 dl 1.1 }
133     }
134     });
135 dl 1.3 try {
136 dl 1.1 t.start();
137     t.join();
138     }
139     catch(Exception e) {
140 dl 1.3 unexpectedException();
141 dl 1.1 }
142     }
143    
144    
145 dl 1.3 /**
146 dl 1.4 * parkUntil times out if not unparked
147 dl 1.3 */
148 dl 1.1 public void testParkUntil() {
149 dl 1.3 Thread t = new Thread(new Runnable() {
150     public void run() {
151     try {
152 dl 1.1 long d = new Date().getTime() + 100;
153     LockSupport.parkUntil(d);
154 dl 1.3 } catch(Exception e){
155     threadUnexpectedException();
156 dl 1.1 }
157     }
158     });
159 dl 1.3 try {
160 dl 1.1 t.start();
161     t.join();
162     }
163     catch(Exception e) {
164 dl 1.3 unexpectedException();
165 dl 1.1 }
166     }
167     }