1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
|
|
* public domain. Use, modify, and redistribute this code in any way |
4 |
|
|
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
|
|
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
import junit.framework.*; |
9 |
|
|
import java.util.*; |
10 |
|
|
import java.util.concurrent.*; |
11 |
|
|
import java.util.concurrent.locks.*; |
12 |
|
|
|
13 |
|
|
public class LockSupportTest extends TestCase{ |
14 |
|
|
|
15 |
|
|
public static void main(String[] args) { |
16 |
|
|
junit.textui.TestRunner.run (suite()); |
17 |
|
|
} |
18 |
|
|
|
19 |
|
|
|
20 |
|
|
public static Test suite() { |
21 |
|
|
return new TestSuite(LockSupportTest.class); |
22 |
|
|
} |
23 |
|
|
|
24 |
|
|
public void testUnpark() { |
25 |
|
|
Thread t = new Thread(new Runnable(){ |
26 |
|
|
public void run(){ |
27 |
|
|
try{ |
28 |
|
|
LockSupport.park(); |
29 |
|
|
}catch(Exception e){ |
30 |
|
|
fail("unexpected exception"); |
31 |
|
|
} |
32 |
|
|
} |
33 |
|
|
}); |
34 |
|
|
t.start(); |
35 |
|
|
try{ |
36 |
|
|
LockSupport.unpark(t); |
37 |
|
|
t.join(); |
38 |
|
|
} |
39 |
|
|
catch(Exception e) { |
40 |
|
|
fail("unexpected exception"); |
41 |
|
|
} |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
public void testParkNanos() { |
45 |
|
|
Thread t = new Thread(new Runnable(){ |
46 |
|
|
public void run(){ |
47 |
|
|
try{ |
48 |
|
|
LockSupport.parkNanos(1000); |
49 |
|
|
}catch(Exception e){ |
50 |
|
|
fail("unexpected exception"); |
51 |
|
|
} |
52 |
|
|
} |
53 |
|
|
}); |
54 |
|
|
try{ |
55 |
|
|
t.start(); |
56 |
|
|
t.join(); |
57 |
|
|
} |
58 |
|
|
catch(Exception e) { |
59 |
|
|
fail("unexpected exception"); |
60 |
|
|
} |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
|
64 |
|
|
public void testParkUntil() { |
65 |
|
|
Thread t = new Thread(new Runnable(){ |
66 |
|
|
public void run(){ |
67 |
|
|
try{ |
68 |
|
|
long d = new Date().getTime() + 100; |
69 |
|
|
LockSupport.parkUntil(d); |
70 |
|
|
}catch(Exception e){ |
71 |
|
|
fail("unexpected exception"); |
72 |
|
|
} |
73 |
|
|
} |
74 |
|
|
}); |
75 |
|
|
try{ |
76 |
|
|
t.start(); |
77 |
|
|
t.join(); |
78 |
|
|
} |
79 |
|
|
catch(Exception e) { |
80 |
|
|
fail("unexpected exception"); |
81 |
|
|
} |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
|
85 |
|
|
} |