ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LockSupportTest.java
(Generate patch)

Comparing jsr166/src/test/tck/LockSupportTest.java (file contents):
Revision 1.2 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.11 by jsr166, Tue Nov 17 12:46:10 2009 UTC

# Line 1 | Line 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.
2 > * 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   */
8  
9   import junit.framework.*;
# Line 10 | Line 11 | import java.util.*;
11   import java.util.concurrent.*;
12   import java.util.concurrent.locks.*;
13  
14 < public class LockSupportTest extends JSR166TestCase{
14 > public class LockSupportTest extends JSR166TestCase {
15      public static void main(String[] args) {
16 <        junit.textui.TestRunner.run (suite());  
16 >        junit.textui.TestRunner.run (suite());
17      }
18      public static Test suite() {
19          return new TestSuite(LockSupportTest.class);
20      }
21  
22 <    public void testUnpark() {
23 <        Thread t = new Thread(new Runnable(){
24 <                public void run(){
25 <                    try{
26 <                        LockSupport.park();
27 <                    }catch(Exception e){
28 <                        threadFail("unexpected exception");
29 <                    }
30 <                }
31 <            });
32 <        t.start();
33 <        try{
34 <            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 <        }
22 >    /**
23 >     * park is released by unpark occurring after park
24 >     */
25 >    public void testPark() throws InterruptedException {
26 >        Thread t = new Thread(new CheckedRunnable() {
27 >            public void realRun() {
28 >                LockSupport.park();
29 >            }});
30 >
31 >        t.start();
32 >        Thread.sleep(SHORT_DELAY_MS);
33 >        LockSupport.unpark(t);
34 >        t.join();
35      }
36  
37 +    /**
38 +     * park is released by unpark occurring before park
39 +     */
40 +    public void testPark2() throws InterruptedException {
41 +        Thread t = new Thread(new CheckedRunnable() {
42 +            public void realRun() throws InterruptedException {
43 +                Thread.sleep(SHORT_DELAY_MS);
44 +                LockSupport.park();
45 +            }});
46 +
47 +        t.start();
48 +        LockSupport.unpark(t);
49 +        t.join();
50 +    }
51 +
52 +    /**
53 +     * park is released by interrupt
54 +     */
55 +    public void testPark3() throws InterruptedException {
56 +        Thread t = new Thread(new CheckedRunnable() {
57 +            public void realRun() {
58 +                LockSupport.park();
59 +            }});
60 +
61 +        t.start();
62 +        Thread.sleep(SHORT_DELAY_MS);
63 +        t.interrupt();
64 +        t.join();
65 +    }
66 +
67 +    /**
68 +     * park returns if interrupted before park
69 +     */
70 +    public void testPark4() throws InterruptedException {
71 +        final ReentrantLock lock = new ReentrantLock();
72 +        lock.lock();
73 +        Thread t = new Thread(new CheckedRunnable() {
74 +            public void realRun() {
75 +                lock.lock();
76 +                LockSupport.park();
77 +            }});
78 +
79 +        t.start();
80 +        t.interrupt();
81 +        lock.unlock();
82 +        t.join();
83 +    }
84 +
85 +    /**
86 +     * parkNanos times out if not unparked
87 +     */
88 +    public void testParkNanos() throws InterruptedException {
89 +        Thread t = new Thread(new CheckedRunnable() {
90 +            public void realRun() {
91 +                LockSupport.parkNanos(1000);
92 +            }});
93 +
94 +        t.start();
95 +        t.join();
96 +    }
97 +
98 +
99 +    /**
100 +     * parkUntil times out if not unparked
101 +     */
102 +    public void testParkUntil() throws InterruptedException {
103 +        Thread t = new Thread(new CheckedRunnable() {
104 +            public void realRun() {
105 +                long d = new Date().getTime() + 100;
106 +                LockSupport.parkUntil(d);
107 +            }});
108  
109 <    public void testParkUntil() {
110 <        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 <        }
109 >        t.start();
110 >        t.join();
111      }
112   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines