ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LockSupportTest.java
Revision: 1.10
Committed: Mon Nov 16 05:30:08 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.9: +7 -7 lines
Log Message:
whitespace

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 jsr166 1.8 * 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 jsr166 1.10 public class LockSupportTest extends JSR166TestCase {
15 dl 1.1 public static void main(String[] args) {
16 jsr166 1.8 junit.textui.TestRunner.run (suite());
17 dl 1.1 }
18     public static Test suite() {
19     return new TestSuite(LockSupportTest.class);
20     }
21    
22 dl 1.3 /**
23 dl 1.6 * park is released by unpark occurring after park
24 dl 1.3 */
25 jsr166 1.8 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 jsr166 1.10 } catch (Exception e) {
31 dl 1.3 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 jsr166 1.9 catch (Exception e) {
42 dl 1.3 unexpectedException();
43 dl 1.1 }
44     }
45    
46 dl 1.3 /**
47 dl 1.6 * park is released by unpark occurring before park
48 dl 1.4 */
49 jsr166 1.8 public void testPark2() {
50 dl 1.4 Thread t = new Thread(new Runnable() {
51     public void run() {
52     try {
53     Thread.sleep(SHORT_DELAY_MS);
54     LockSupport.park();
55 jsr166 1.10 } catch (Exception e) {
56 dl 1.4 threadUnexpectedException();
57     }
58     }
59     });
60     try {
61     t.start();
62     LockSupport.unpark(t);
63     t.join();
64     }
65 jsr166 1.9 catch (Exception e) {
66 dl 1.4 unexpectedException();
67     }
68     }
69    
70     /**
71 jsr166 1.8 * park is released by interrupt
72 dl 1.4 */
73 jsr166 1.8 public void testPark3() {
74 dl 1.4 Thread t = new Thread(new Runnable() {
75     public void run() {
76     try {
77     LockSupport.park();
78 jsr166 1.10 } catch (Exception e) {
79 dl 1.4 threadUnexpectedException();
80     }
81     }
82     });
83     try {
84     t.start();
85     Thread.sleep(SHORT_DELAY_MS);
86     t.interrupt();
87     t.join();
88     }
89 jsr166 1.9 catch (Exception e) {
90 dl 1.4 unexpectedException();
91     }
92     }
93    
94     /**
95     * park returns if interrupted before park
96     */
97 jsr166 1.8 public void testPark4() {
98 dl 1.4 final ReentrantLock lock = new ReentrantLock();
99     lock.lock();
100     Thread t = new Thread(new Runnable() {
101     public void run() {
102     try {
103     lock.lock();
104     LockSupport.park();
105 jsr166 1.10 } catch (Exception e) {
106 dl 1.4 threadUnexpectedException();
107     }
108     }
109     });
110     try {
111     t.start();
112     t.interrupt();
113     lock.unlock();
114     t.join();
115     }
116 jsr166 1.9 catch (Exception e) {
117 dl 1.4 unexpectedException();
118     }
119     }
120    
121     /**
122     * parkNanos times out if not unparked
123 dl 1.3 */
124 jsr166 1.8 public void testParkNanos() {
125 dl 1.3 Thread t = new Thread(new Runnable() {
126     public void run() {
127     try {
128 dl 1.1 LockSupport.parkNanos(1000);
129 jsr166 1.10 } catch (Exception e) {
130 dl 1.3 threadUnexpectedException();
131 dl 1.1 }
132     }
133     });
134 dl 1.3 try {
135 dl 1.1 t.start();
136     t.join();
137     }
138 jsr166 1.9 catch (Exception e) {
139 dl 1.3 unexpectedException();
140 dl 1.1 }
141     }
142    
143    
144 dl 1.3 /**
145 dl 1.4 * parkUntil times out if not unparked
146 dl 1.3 */
147 jsr166 1.8 public void testParkUntil() {
148 dl 1.3 Thread t = new Thread(new Runnable() {
149     public void run() {
150     try {
151 dl 1.1 long d = new Date().getTime() + 100;
152     LockSupport.parkUntil(d);
153 jsr166 1.10 } catch (Exception e) {
154 dl 1.3 threadUnexpectedException();
155 dl 1.1 }
156     }
157     });
158 dl 1.3 try {
159 dl 1.1 t.start();
160     t.join();
161     }
162 jsr166 1.9 catch (Exception e) {
163 dl 1.3 unexpectedException();
164 dl 1.1 }
165     }
166     }