1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
   4  */
   5 package jdk.testlibrary;
   6 
   7 import java.lang.management.ManagementFactory;
   8 import java.lang.management.ThreadInfo;
   9 import java.lang.management.ThreadMXBean;
  10 import java.util.concurrent.TimeoutException;
  11 
  12 public class TestThread extends Thread {
  13 
  14     private Runnable runnable;
  15 
  16     public Runnable getRunnable() {
  17         return runnable;
  18     }
  19 
  20     public TestThread(Runnable target, String name) {
  21         super(target, name);
  22         this.runnable = target;
  23     }
  24 
  25     public TestThread(Runnable target) {
  26         super(target);
  27         this.runnable = target;
  28     }
  29 
  30     public TestThread(ThreadGroup group, Runnable target, String name,
  31             long stackSize) {
  32         super(group, target, name, stackSize);
  33         this.runnable = target;
  34     }
  35 
  36     public TestThread(ThreadGroup group, Runnable target, String name) {
  37         super(group, target, name);
  38         this.runnable = target;
  39     }
  40 
  41     public TestThread(ThreadGroup group, Runnable target) {
  42         super(group, target);
  43         this.runnable = target;
  44     }
  45 
  46     private Throwable uncaught;
  47 
  48     @Override
  49     public void run() {
  50         try {
  51             super.run();
  52         } catch (Throwable t) {
  53             uncaught = t;
  54         }
  55     }
  56 
  57     public Throwable getUncaught() {
  58         return uncaught;
  59     }
  60 
  61     public void joinAndThrow() throws InterruptedException, Throwable {
  62         join();
  63         if (uncaught != null) {
  64             throw uncaught;
  65         }
  66     }
  67 
  68     public void joinAndThrow(long timeout) throws InterruptedException,
  69             Throwable {
  70         join(timeout);
  71         if (isAlive()) {
  72             throw new TimeoutException();
  73         }
  74         if (uncaught != null) {
  75             throw uncaught;
  76         }
  77     }
  78 
  79     public Throwable joinAndReturn() throws InterruptedException {
  80         join();
  81         if (uncaught != null) {
  82             return uncaught;
  83         }
  84         return null;
  85     }
  86 
  87     public Throwable joinAndReturn(long timeout) throws InterruptedException {
  88         join(timeout);
  89         if (isAlive()) {
  90             return new TimeoutException();
  91         }
  92         if (uncaught != null) {
  93             return uncaught;
  94         }
  95         return null;
  96     }
  97 
  98     /**
  99      * Use java.lang.management to find out if this thread is waiting
 100      */
 101     public void waitUntilBlockingOnObject(Thread.State state, Object o) {
 102         String want = o == null ? null : o.getClass().getName() + '@'
 103                 + Integer.toHexString(System.identityHashCode(o));
 104         ThreadMXBean tmx = ManagementFactory.getThreadMXBean();
 105         while (isAlive()) {
 106             ThreadInfo ti = tmx.getThreadInfo(getId());
 107             if (ti.getThreadState() == state
 108                     && (want == null || want.equals(ti.getLockName()))) {
 109                 return;
 110             }
 111             try {
 112                 Thread.sleep(1);
 113             } catch (InterruptedException e) {
 114             }
 115         }
 116     }
 117 
 118     public void waitUntilInNative() {
 119         ThreadMXBean tmx = ManagementFactory.getThreadMXBean();
 120         while (isAlive()) {
 121             ThreadInfo ti = tmx.getThreadInfo(getId());
 122             if (ti.isInNative()) {
 123                 return;
 124             }
 125             try {
 126                 Thread.sleep(1);
 127             } catch (InterruptedException e) {
 128             }
 129         }
 130     }
 131 
 132 }