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 JSR166TestCase{ |
14 |
public static void main(String[] args) { |
15 |
junit.textui.TestRunner.run (suite()); |
16 |
} |
17 |
public static Test suite() { |
18 |
return new TestSuite(LockSupportTest.class); |
19 |
} |
20 |
|
21 |
public void testUnpark() { |
22 |
Thread t = new Thread(new Runnable(){ |
23 |
public void run(){ |
24 |
try{ |
25 |
LockSupport.park(); |
26 |
}catch(Exception e){ |
27 |
threadFail("unexpected exception"); |
28 |
} |
29 |
} |
30 |
}); |
31 |
t.start(); |
32 |
try{ |
33 |
LockSupport.unpark(t); |
34 |
t.join(); |
35 |
} |
36 |
catch(Exception e) { |
37 |
fail("unexpected exception"); |
38 |
} |
39 |
} |
40 |
|
41 |
public void testParkNanos() { |
42 |
Thread t = new Thread(new Runnable(){ |
43 |
public void run(){ |
44 |
try{ |
45 |
LockSupport.parkNanos(1000); |
46 |
}catch(Exception e){ |
47 |
threadFail("unexpected exception"); |
48 |
} |
49 |
} |
50 |
}); |
51 |
try{ |
52 |
t.start(); |
53 |
t.join(); |
54 |
} |
55 |
catch(Exception e) { |
56 |
fail("unexpected exception"); |
57 |
} |
58 |
} |
59 |
|
60 |
|
61 |
public void testParkUntil() { |
62 |
Thread t = new Thread(new Runnable(){ |
63 |
public void run(){ |
64 |
try{ |
65 |
long d = new Date().getTime() + 100; |
66 |
LockSupport.parkUntil(d); |
67 |
}catch(Exception e){ |
68 |
threadFail("unexpected exception"); |
69 |
} |
70 |
} |
71 |
}); |
72 |
try{ |
73 |
t.start(); |
74 |
t.join(); |
75 |
} |
76 |
catch(Exception e) { |
77 |
fail("unexpected exception"); |
78 |
} |
79 |
} |
80 |
} |