1 package gov.nasa.jpf.jvm; 20 21 24 public class TestThread { 25 static String didRunThread = null; 26 27 public static void main (String [] args) { 28 TestThread t = new TestThread(); 29 30 if (args.length > 0) { 31 for (int i = 0; i < args.length; i++) { 33 String func = args[i]; 34 35 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 ("unknown test function"); 51 } 52 } 53 } else { 54 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 Runnable r = new Runnable () { 68 public void run () { 69 Thread t = Thread.currentThread(); 70 71 if (!t.isDaemon()) { 72 throw new RuntimeException ("isDaemon failed"); 73 } 74 75 didRunThread = t.getName(); 76 } 77 }; 78 79 didRunThread = null; 80 81 Thread t = new Thread (r); 82 t.setDaemon(true); 83 t.start(); 84 85 try { 86 t.join(); 87 } catch (InterruptedException ix) { 88 throw new RuntimeException ("thread was interrupted"); 89 } 90 91 String tname = Thread.currentThread().getName(); 92 if ((didRunThread == null) || (didRunThread.equals(tname))) { 93 throw new RuntimeException ("thread did not execute: " + didRunThread); 94 } 95 } 96 97 public void testMain () { 98 Thread t = Thread.currentThread(); 99 String refName = "main"; 100 101 String name = t.getName(); 102 103 if (!name.equals(refName)) { 104 throw new RuntimeException ("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 ("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 ("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 ("main thread setPriority failed: " + prio); 130 } 131 132 if (t.isDaemon()) { 133 throw new RuntimeException ("main thread is daemon"); 134 } 135 } 136 137 public void testName () { 138 Runnable r = new Runnable () { 139 public void run () { 140 Thread t = Thread.currentThread(); 141 String name = t.getName(); 142 143 if (!name.equals("my-thread")) { 144 throw new RuntimeException ("wrong Thread name: " + name); 145 } 146 147 didRunThread = name; 148 } 149 }; 150 151 didRunThread = null; 152 153 Thread t = new Thread (r, "my-thread"); 154 155 156 t.start(); 158 159 try { 160 t.join(); 161 } catch (InterruptedException ix) { 162 throw new RuntimeException ("thread was interrupted"); 163 } 164 165 String tname = Thread.currentThread().getName(); 166 if ((didRunThread==null) || (didRunThread.equals(tname))) { 167 throw new RuntimeException ("thread did not execute: " + didRunThread); 168 } 169 } 170 171 public void testSingleYield () { 172 Thread.yield(); 173 } 174 175 public void testYield () { 176 Runnable r = new Runnable () { 177 public void run () { 178 Thread 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 t = new Thread (r); 191 t.start(); 192 193 while (didRunThread.equals("blah")) { 194 Thread.yield(); 195 } 196 197 String tname = Thread.currentThread().getName(); 198 if ((didRunThread == null) || (didRunThread.equals(tname))) { 199 throw new RuntimeException ("thread did not execute: " + didRunThread); 200 } 201 } 202 203 public void testPriority () { 204 Runnable r = new Runnable () { 205 public void run () { 206 Thread t = Thread.currentThread(); 207 int prio = t.getPriority(); 208 209 if (prio != (Thread.MIN_PRIORITY + 2)) { 210 throw new RuntimeException ("wrong Thread priority: " + prio); 211 } 212 213 didRunThread = t.getName(); 214 } 215 }; 216 217 didRunThread = null; 218 219 Thread t = new Thread (r); 220 t.setPriority(Thread.MIN_PRIORITY + 2); 221 t.start(); 222 223 try { 224 t.join(); 225 } catch (InterruptedException ix) { 226 throw new RuntimeException ("thread was interrupted"); 227 } 228 229 String tname = Thread.currentThread().getName(); 230 if ((didRunThread == null) || (didRunThread.equals(tname))) { 231 throw new RuntimeException ("thread did not execute: " + didRunThread); 232 } 233 } 234 } 235 | Popular Tags |