KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > jvm > TestWait


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.jvm;
20
21 /**
22  * signal test (wait/notify)
23  */

24 public class TestWait
25 {
26   boolean cond;
27   boolean done;
28   
29   public static void main (String JavaDoc[] args) {
30     TestWait t = new TestWait();
31
32     if (args.length > 0) {
33       // just run the specified tests
34
for (int i = 0; i < args.length; i++) {
35         String JavaDoc func = args[i];
36
37         // note that we don't use reflection here because this would
38
// blow up execution/test scope under JPF
39
if ("testSimpleWait".equals(func)) {
40           t.testSimpleWait();
41         } else if ("testLoopedWait".equals(func)) {
42           t.testLoopedWait();
43         } else if ("testInterruptedWait".equals(func)){
44           t.testInterruptedWait();
45         } else {
46           throw new IllegalArgumentException JavaDoc("unknown test function");
47         }
48       }
49     } else {
50       // quite useless to call the others, it locks up
51
t.testSimpleWait();
52       t.testLoopedWait();
53       t.testInterruptedWait();
54     }
55   }
56
57   public void testSimpleWait () {
58     Runnable JavaDoc notifier = new Runnable JavaDoc() {
59       public void run () {
60         synchronized (TestWait.this) {
61           System.out.println( "notifying");
62           cond = true;
63           TestWait.this.notify();
64         }
65       }
66     };
67     
68     Thread JavaDoc t = new Thread JavaDoc(notifier);
69
70     cond = false;
71     
72     synchronized (this) {
73       t.start();
74     
75       try {
76         System.out.println("waiting");
77         wait();
78         System.out.println("notified");
79         if (!cond) {
80           throw new RuntimeException JavaDoc("'cond' not set, premature wait return");
81         }
82       } catch (InterruptedException JavaDoc ix) {
83       }
84     }
85   }
86   
87   public void testLoopedWait () {
88     Runnable JavaDoc notifier = new Runnable JavaDoc() {
89       public void run () {
90         while (!done) {
91           synchronized (TestWait.this) {
92             System.out.println( "notifying");
93             cond = true;
94             TestWait.this.notify();
95           }
96         }
97       }
98     };
99     
100     Thread JavaDoc t = new Thread JavaDoc(notifier);
101
102     cond = false;
103     done = false;
104
105     t.start();
106     synchronized (this) {
107       for (int i=0; i<5; i++) {
108         try {
109           System.out.println("waiting " + i);
110           wait();
111           System.out.println("notified " + i);
112           if (!cond) {
113             throw new RuntimeException JavaDoc("'cond' not set, premature wait return");
114           }
115           cond = false;
116         } catch (InterruptedException JavaDoc ix) {}
117       }
118       done = true;
119     }
120   }
121   
122   public void testInterruptedWait () {
123     final Thread JavaDoc current = Thread.currentThread();
124     
125     Runnable JavaDoc notifier = new Runnable JavaDoc() {
126       public void run () {
127         synchronized (TestWait.this) {
128           System.out.println( "interrupting");
129           cond = true;
130           current.interrupt();
131         }
132       }
133     };
134     Thread JavaDoc t = new Thread JavaDoc(notifier);
135
136     cond = false;
137     
138     synchronized (this) {
139       t.start();
140     
141       try {
142         System.out.println("waiting");
143         wait();
144         System.out.println("notified");
145         throw new RuntimeException JavaDoc("notified, not interrupted");
146       } catch (InterruptedException JavaDoc ix) {
147         if (!cond) {
148           throw new RuntimeException JavaDoc("'cond' not set, premature wait return");
149         }
150       }
151     }
152   }
153 }
154
155
Popular Tags