6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4967283 5080203 27 * @summary Basic unit test of thread states returned by 28 * ThreadMXBean.getThreadInfo.getThreadState(). 29 * It also tests lock information returned by ThreadInfo. 30 * 31 * @author Mandy Chung 32 * 33 * @build ThreadExecutionSynchronizer Utils 34 * @run main ThreadStateTest 35 */ 36 37 import java.lang.management.ManagementFactory; 38 import java.lang.management.ThreadMXBean; 39 import java.lang.management.ThreadInfo; 40 41 import java.util.concurrent.locks.LockSupport; 42 43 public class ThreadStateTest { 44 private static final ThreadMXBean tm = ManagementFactory.getThreadMXBean(); 45 46 static class Lock { 47 private String name; 48 Lock(String name) { 49 this.name = name; 50 } 51 public String toString() { 52 return name; 53 } 54 } 55 private static Lock globalLock = new Lock("my lock"); 56 57 public static void main(String[] argv) { 58 // Force thread state initialization now before the test 59 // verification begins. 60 Thread.currentThread().getState(); 61 62 MyThread myThread = new MyThread("MyThread"); 63 64 // before myThread starts 65 // Utils.checkThreadState(myThread, Thread.State.NEW); 66 67 myThread.start(); 68 myThread.waitUntilStarted(); 69 Utils.checkThreadState(myThread, Thread.State.RUNNABLE); 70 checkLockInfo(myThread, Thread.State.RUNNABLE, null, null); 71 72 myThread.suspend(); 73 Utils.goSleep(10); 74 checkSuspendedThreadState(myThread, Thread.State.RUNNABLE); 75 myThread.resume(); 76 77 synchronized (globalLock) { 78 myThread.goBlocked(); 79 Utils.checkThreadState(myThread, Thread.State.BLOCKED); 80 checkLockInfo(myThread, Thread.State.BLOCKED, 81 globalLock, Thread.currentThread()); 82 } 205 " to be -1 but got " + info.getLockOwnerId()); 206 } 207 208 if (owner != null && info.getLockOwnerId() <= 0) { 209 throw new RuntimeException("Lock owner is expected to be " + 210 owner.getName() + "(id = " + owner.getId() + 211 ") but got " + info.getLockOwnerId()); 212 } 213 if (owner != null && info.getLockOwnerId() != owner.getId()) { 214 throw new RuntimeException("Lock owner is expected to be " + 215 owner.getName() + "(id = " + owner.getId() + 216 ") but got " + info.getLockOwnerId()); 217 } 218 if (info.isSuspended()) { 219 throw new RuntimeException(t.getName() + 220 " isSuspended() returns " + info.isSuspended()); 221 } 222 } 223 224 static class MyThread extends Thread { 225 private ThreadExecutionSynchronizer thrsync = new ThreadExecutionSynchronizer(); 226 227 MyThread(String name) { 228 super(name); 229 } 230 231 private final int RUNNABLE = 0; 232 private final int BLOCKED = 1; 233 private final int WAITING = 2; 234 private final int TIMED_WAITING = 3; 235 private final int PARKED = 4; 236 private final int TIMED_PARKED = 5; 237 private final int SLEEPING = 6; 238 private final int TERMINATE = 7; 239 private int state = RUNNABLE; 240 241 private boolean done = false; 242 public void run() { 243 // Signal main thread to continue. 244 thrsync.signal(); 245 while (!done) { 246 switch (state) { 247 case RUNNABLE: { 248 double sum = 0; 249 for (int i = 0; i < 1000; i++) { 250 double r = Math.random(); 251 double x = Math.pow(3, r); 252 sum += x - r; 253 } 254 break; 255 } 256 case BLOCKED: { 257 // signal main thread. 258 thrsync.signal(); 259 System.out.println(" myThread is going to block."); 260 synchronized (globalLock) { 261 // finish blocking 262 state = RUNNABLE; 263 } 264 break; 265 } 266 case WAITING: { 267 synchronized (globalLock) { 268 // signal main thread. 269 thrsync.signal(); 270 System.out.println(" myThread is going to wait."); 271 try { 272 globalLock.wait(); 273 } catch (InterruptedException e) { 274 // ignore 275 } 276 } 277 break; 278 } 279 case TIMED_WAITING: { 280 synchronized (globalLock) { 281 // signal main thread. 282 thrsync.signal(); 283 System.out.println(" myThread is going to timed wait."); 284 try { 285 globalLock.wait(10000); 286 } catch (InterruptedException e) { 287 // ignore 288 } 289 } 290 break; 291 } 292 case PARKED: { 293 // signal main thread. 294 thrsync.signal(); 295 System.out.println(" myThread is going to park."); 296 LockSupport.park(); 297 // give a chance for the main thread to block 298 System.out.println(" myThread is going to park."); 299 Utils.goSleep(10); 300 break; 301 } 302 case TIMED_PARKED: { 303 // signal main thread. 304 thrsync.signal(); 305 System.out.println(" myThread is going to timed park."); 306 long deadline = System.currentTimeMillis() + 10000*1000; 307 LockSupport.parkUntil(deadline); 308 309 // give a chance for the main thread to block 310 Utils.goSleep(10); 311 break; 312 } 313 case SLEEPING: { 314 // signal main thread. 315 thrsync.signal(); 316 System.out.println(" myThread is going to sleep."); 317 try { 318 Thread.sleep(1000000); 319 } catch (InterruptedException e) { 320 // finish sleeping 321 interrupted(); 322 } 323 break; 324 } 325 case TERMINATE: { 326 done = true; 327 // signal main thread. 328 thrsync.signal(); 329 break; 330 } 331 default: 332 break; 333 } 334 } 335 } 336 public void waitUntilStarted() { 337 // wait for MyThread. 338 thrsync.waitForSignal(); 339 Utils.goSleep(10); 340 } 341 342 public void goBlocked() { 343 System.out.println("Waiting myThread to go blocked."); 344 setState(BLOCKED); 345 // wait for MyThread to get blocked 346 thrsync.waitForSignal(); 347 Utils.goSleep(20); 348 } 349 350 public void goWaiting() { 351 System.out.println("Waiting myThread to go waiting."); 352 setState(WAITING); 353 // wait for MyThread to wait on object. 354 thrsync.waitForSignal(); 355 Utils.goSleep(20); 356 } 357 public void goTimedWaiting() { 358 System.out.println("Waiting myThread to go timed waiting."); 359 setState(TIMED_WAITING); 360 // wait for MyThread timed wait call. 361 thrsync.waitForSignal(); 362 Utils.goSleep(20); 363 } 364 public void goParked() { 365 System.out.println("Waiting myThread to go parked."); 366 setState(PARKED); 367 // wait for MyThread state change to PARKED. 368 thrsync.waitForSignal(); 369 Utils.goSleep(20); 370 } 371 public void goTimedParked() { 372 System.out.println("Waiting myThread to go timed parked."); 373 setState(TIMED_PARKED); 374 // wait for MyThread. 375 thrsync.waitForSignal(); 376 Utils.goSleep(20); 377 } 378 379 public void goSleeping() { 380 System.out.println("Waiting myThread to go sleeping."); 381 setState(SLEEPING); 382 // wait for MyThread. 383 thrsync.waitForSignal(); 384 Utils.goSleep(20); 385 } 386 public void terminate() { 387 System.out.println("Waiting myThread to terminate."); 388 setState(TERMINATE); 389 // wait for MyThread. 390 thrsync.waitForSignal(); 391 Utils.goSleep(20); 392 } 393 394 private void setState(int newState) { 395 switch (state) { 396 case BLOCKED: 397 while (state == BLOCKED) { 398 Utils.goSleep(20); 399 } 400 state = newState; 401 break; 402 case WAITING: 403 case TIMED_WAITING: 404 state = newState; 405 synchronized (globalLock) { 406 globalLock.notify(); 407 } 408 break; 409 case PARKED: 410 case TIMED_PARKED: 411 state = newState; | 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4967283 5080203 8022208 27 * @summary Basic unit test of thread states returned by 28 * ThreadMXBean.getThreadInfo.getThreadState(). 29 * It also tests lock information returned by ThreadInfo. 30 * 31 * @author Mandy Chung 32 * 33 * @build Utils 34 * @run main ThreadStateTest 35 */ 36 37 import java.lang.management.ManagementFactory; 38 import java.lang.management.ThreadMXBean; 39 import java.lang.management.ThreadInfo; 40 import java.util.concurrent.Phaser; 41 import java.util.concurrent.locks.LockSupport; 42 43 public class ThreadStateTest { 44 private static final ThreadMXBean tm = ManagementFactory.getThreadMXBean(); 45 46 static class Lock { 47 private String name; 48 Lock(String name) { 49 this.name = name; 50 } 51 public String toString() { 52 return name; 53 } 54 } 55 private static Lock globalLock = new Lock("my lock"); 56 57 public static void main(String[] argv) { 58 // Force thread state initialization now before the test 59 // verification begins. 60 Thread.currentThread().getState(); 61 62 MyThread myThread = new MyThread("MyThread"); 63 myThread.setDaemon(true); 64 65 // before myThread starts 66 // Utils.checkThreadState(myThread, Thread.State.NEW); 67 68 myThread.start(); 69 myThread.waitUntilStarted(); 70 Utils.checkThreadState(myThread, Thread.State.RUNNABLE); 71 checkLockInfo(myThread, Thread.State.RUNNABLE, null, null); 72 73 myThread.suspend(); 74 Utils.goSleep(10); 75 checkSuspendedThreadState(myThread, Thread.State.RUNNABLE); 76 myThread.resume(); 77 78 synchronized (globalLock) { 79 myThread.goBlocked(); 80 Utils.checkThreadState(myThread, Thread.State.BLOCKED); 81 checkLockInfo(myThread, Thread.State.BLOCKED, 82 globalLock, Thread.currentThread()); 83 } 206 " to be -1 but got " + info.getLockOwnerId()); 207 } 208 209 if (owner != null && info.getLockOwnerId() <= 0) { 210 throw new RuntimeException("Lock owner is expected to be " + 211 owner.getName() + "(id = " + owner.getId() + 212 ") but got " + info.getLockOwnerId()); 213 } 214 if (owner != null && info.getLockOwnerId() != owner.getId()) { 215 throw new RuntimeException("Lock owner is expected to be " + 216 owner.getName() + "(id = " + owner.getId() + 217 ") but got " + info.getLockOwnerId()); 218 } 219 if (info.isSuspended()) { 220 throw new RuntimeException(t.getName() + 221 " isSuspended() returns " + info.isSuspended()); 222 } 223 } 224 225 static class MyThread extends Thread { 226 // Phaser to sync between the main thread putting 227 // this thread into various states 228 private Phaser phaser = new Phaser(2); 229 230 MyThread(String name) { 231 super(name); 232 } 233 234 private final int RUNNABLE = 0; 235 private final int BLOCKED = 1; 236 private final int WAITING = 2; 237 private final int TIMED_WAITING = 3; 238 private final int PARKED = 4; 239 private final int TIMED_PARKED = 5; 240 private final int SLEEPING = 6; 241 private final int TERMINATE = 7; 242 private volatile int state = RUNNABLE; 243 private boolean done = false; 244 public void run() { 245 // Signal main thread to continue. 246 phaser.arriveAndAwaitAdvance(); 247 248 while (!done) { 249 switch (state) { 250 case RUNNABLE: { 251 double sum = 0; 252 for (int i = 0; i < 1000; i++) { 253 double r = Math.random(); 254 double x = Math.pow(3, r); 255 sum += x - r; 256 } 257 break; 258 } 259 case BLOCKED: { 260 // signal main thread. 261 phaser.arrive(); 262 System.out.println(" myThread is going to block."); 263 synchronized (globalLock) { 264 // finish blocking 265 state = RUNNABLE; 266 } 267 break; 268 } 269 case WAITING: { 270 synchronized (globalLock) { 271 // signal main thread. 272 phaser.arrive(); 273 System.out.println(" myThread is going to wait."); 274 try { 275 globalLock.wait(); 276 } catch (InterruptedException e) { 277 // ignore 278 } 279 } 280 break; 281 } 282 case TIMED_WAITING: { 283 synchronized (globalLock) { 284 // signal main thread. 285 phaser.arrive(); 286 System.out.println(" myThread is going to timed wait."); 287 try { 288 globalLock.wait(10000); 289 } catch (InterruptedException e) { 290 // ignore 291 } 292 } 293 break; 294 } 295 case PARKED: { 296 // signal main thread. 297 phaser.arrive(); 298 System.out.println(" myThread is going to park."); 299 LockSupport.park(); 300 // give a chance for the main thread to block 301 Utils.goSleep(10); 302 break; 303 } 304 case TIMED_PARKED: { 305 // signal main thread. 306 phaser.arrive(); 307 System.out.println(" myThread is going to timed park."); 308 long deadline = System.currentTimeMillis() + 10000*1000; 309 LockSupport.parkUntil(deadline); 310 311 // give a chance for the main thread to block 312 Utils.goSleep(10); 313 break; 314 } 315 case SLEEPING: { 316 // signal main thread. 317 phaser.arrive(); 318 System.out.println(" myThread is going to sleep."); 319 try { 320 Thread.sleep(1000000); 321 } catch (InterruptedException e) { 322 // finish sleeping 323 interrupted(); 324 } 325 break; 326 } 327 case TERMINATE: { 328 done = true; 329 // signal main thread. 330 phaser.arrive(); 331 break; 332 } 333 default: 334 break; 335 } 336 } 337 } 338 339 public void waitUntilStarted() { 340 // wait for MyThread. 341 phaser.arriveAndAwaitAdvance(); 342 } 343 344 public void goBlocked() { 345 System.out.println("Waiting myThread to go blocked."); 346 setState(BLOCKED); 347 // wait for MyThread to get to a point just before being blocked 348 phaser.arriveAndAwaitAdvance(); 349 } 350 351 public void goWaiting() { 352 System.out.println("Waiting myThread to go waiting."); 353 setState(WAITING); 354 // wait for MyThread to get to just before wait on object. 355 phaser.arriveAndAwaitAdvance(); 356 } 357 358 public void goTimedWaiting() { 359 System.out.println("Waiting myThread to go timed waiting."); 360 setState(TIMED_WAITING); 361 // wait for MyThread to get to just before timed wait call. 362 phaser.arriveAndAwaitAdvance(); 363 } 364 365 public void goParked() { 366 System.out.println("Waiting myThread to go parked."); 367 setState(PARKED); 368 // wait for MyThread to get to just before parked. 369 phaser.arriveAndAwaitAdvance(); 370 } 371 372 public void goTimedParked() { 373 System.out.println("Waiting myThread to go timed parked."); 374 setState(TIMED_PARKED); 375 // wait for MyThread to get to just before timed park. 376 phaser.arriveAndAwaitAdvance(); 377 } 378 379 public void goSleeping() { 380 System.out.println("Waiting myThread to go sleeping."); 381 setState(SLEEPING); 382 // wait for MyThread to get to just before sleeping 383 phaser.arriveAndAwaitAdvance(); 384 } 385 386 public void terminate() { 387 System.out.println("Waiting myThread to terminate."); 388 setState(TERMINATE); 389 // wait for MyThread to get to just before terminate 390 phaser.arriveAndAwaitAdvance(); 391 } 392 393 private void setState(int newState) { 394 switch (state) { 395 case BLOCKED: 396 while (state == BLOCKED) { 397 Utils.goSleep(20); 398 } 399 state = newState; 400 break; 401 case WAITING: 402 case TIMED_WAITING: 403 state = newState; 404 synchronized (globalLock) { 405 globalLock.notify(); 406 } 407 break; 408 case PARKED: 409 case TIMED_PARKED: 410 state = newState; |