KickJava   Java API By Example, From Geeks To Geeks.

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


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  * threading test
23  */

24 public class TestThread {
25   static String JavaDoc didRunThread = null;
26
27   public static void main (String JavaDoc[] args) {
28     TestThread t = new TestThread();
29
30     if (args.length > 0) {
31       // just run the specified tests
32
for (int i = 0; i < args.length; i++) {
33         String JavaDoc func = args[i];
34
35         // note that we don't use reflection here because this would
36
// blow up execution/test scope under JPF
37
if ("testMain".equals(func)) {
38           t.testMain();
39         } else if ("testName".equals(func)) {
40           t.testName();
41         } else if ("testPriority".equals(func)) {
42           t.testPriority();
43         } else if ("testDaemon".equals(func)) {
44           t.testDaemon();
45         } else if ("testSingleYield".equals(func)) {
46           t.testSingleYield();
47         } else if ("testYield".equals(func)) {
48           t.testYield();
49         } else {
50           throw new IllegalArgumentException JavaDoc("unknown test function");
51         }
52       }
53     } else {
54       // that's mainly for our standalone test verification
55
t.testMain();
56       t.testName();
57       t.testPriority();
58       t.testDaemon();
59       t.testSingleYield();
60       t.testYield();
61     }
62   }
63
64   public void testDaemon () {
65     // we should also test the correct Daemon behavior at some point
66
// (running daemons not keeping the system alive)
67
Runnable JavaDoc r = new Runnable JavaDoc() {
68       public void run () {
69         Thread JavaDoc t = Thread.currentThread();
70
71         if (!t.isDaemon()) {
72           throw new RuntimeException JavaDoc("isDaemon failed");
73         }
74
75         didRunThread = t.getName();
76       }
77     };
78
79     didRunThread = null;
80
81     Thread JavaDoc t = new Thread JavaDoc(r);
82     t.setDaemon(true);
83     t.start();
84
85     try {
86       t.join();
87     } catch (InterruptedException JavaDoc ix) {
88       throw new RuntimeException JavaDoc("thread was interrupted");
89     }
90
91     String JavaDoc tname = Thread.currentThread().getName();
92     if ((didRunThread == null) || (didRunThread.equals(tname))) {
93       throw new RuntimeException JavaDoc("thread did not execute: " + didRunThread);
94     }
95   }
96
97   public void testMain () {
98     Thread JavaDoc t = Thread.currentThread();
99     String JavaDoc refName = "main";
100
101     String JavaDoc name = t.getName();
102
103     if (!name.equals(refName)) {
104       throw new RuntimeException JavaDoc("wrong main thread name, is: " + name +
105                                  ", expected: " + refName);
106     }
107
108     refName = "my-main-thread";
109     t.setName(refName);
110     name = t.getName();
111
112     if (!name.equals(refName)) {
113       throw new RuntimeException JavaDoc("Thread.setName() failed, is: " + name +
114                                  ", expected: " + refName);
115     }
116
117     int refPrio = Thread.NORM_PRIORITY;
118     int prio = t.getPriority();
119
120     if (prio != refPrio) {
121       throw new RuntimeException JavaDoc("main thread has no NORM_PRIORITY: " + prio);
122     }
123
124     refPrio++;
125     t.setPriority(refPrio);
126     prio = t.getPriority();
127
128     if (prio != refPrio) {
129       throw new RuntimeException JavaDoc("main thread setPriority failed: " + prio);
130     }
131
132     if (t.isDaemon()) {
133       throw new RuntimeException JavaDoc("main thread is daemon");
134     }
135   }
136
137   public void testName () {
138     Runnable JavaDoc r = new Runnable JavaDoc() {
139       public void run () {
140         Thread JavaDoc t = Thread.currentThread();
141         String JavaDoc name = t.getName();
142
143         if (!name.equals("my-thread")) {
144           throw new RuntimeException JavaDoc("wrong Thread name: " + name);
145         }
146
147         didRunThread = name;
148       }
149     };
150
151     didRunThread = null;
152
153     Thread JavaDoc t = new Thread JavaDoc(r, "my-thread");
154
155
156     //Thread t = new Thread(r);
157
t.start();
158
159     try {
160       t.join();
161     } catch (InterruptedException JavaDoc ix) {
162       throw new RuntimeException JavaDoc("thread was interrupted");
163     }
164
165     String JavaDoc tname = Thread.currentThread().getName();
166     if ((didRunThread==null) || (didRunThread.equals(tname))) {
167       throw new RuntimeException JavaDoc("thread did not execute: " + didRunThread);
168     }
169   }
170
171   public void testSingleYield () {
172     Thread.yield();
173   }
174   
175   public void testYield () {
176     Runnable JavaDoc r = new Runnable JavaDoc() {
177       public void run () {
178         Thread JavaDoc t = Thread.currentThread();
179
180         while (!didRunThread.equals("blah")) {
181           Thread.yield();
182         }
183         
184         didRunThread = t.getName();
185       }
186     };
187
188     didRunThread = "blah";
189
190     Thread JavaDoc t = new Thread JavaDoc(r);
191     t.start();
192     
193     while (didRunThread.equals("blah")) {
194       Thread.yield();
195     }
196     
197     String JavaDoc tname = Thread.currentThread().getName();
198     if ((didRunThread == null) || (didRunThread.equals(tname))) {
199       throw new RuntimeException JavaDoc("thread did not execute: " + didRunThread);
200     }
201   }
202   
203   public void testPriority () {
204     Runnable JavaDoc r = new Runnable JavaDoc() {
205       public void run () {
206         Thread JavaDoc t = Thread.currentThread();
207         int prio = t.getPriority();
208
209         if (prio != (Thread.MIN_PRIORITY + 2)) {
210           throw new RuntimeException JavaDoc("wrong Thread priority: " + prio);
211         }
212
213         didRunThread = t.getName();
214       }
215     };
216
217     didRunThread = null;
218
219     Thread JavaDoc t = new Thread JavaDoc(r);
220     t.setPriority(Thread.MIN_PRIORITY + 2);
221     t.start();
222
223     try {
224       t.join();
225     } catch (InterruptedException JavaDoc ix) {
226       throw new RuntimeException JavaDoc("thread was interrupted");
227     }
228
229     String JavaDoc tname = Thread.currentThread().getName();
230     if ((didRunThread == null) || (didRunThread.equals(tname))) {
231       throw new RuntimeException JavaDoc("thread did not execute: " + didRunThread);
232     }
233   }
234 }
235
Popular Tags