< prev index next >

test/lib/testlibrary/jdk/testlibrary/TestThread.java

Print this page




   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 package jdk.testlibrary;
  25 
  26 import java.lang.management.ManagementFactory;
  27 import java.lang.management.ThreadInfo;
  28 import java.lang.management.ThreadMXBean;
  29 import java.util.concurrent.TimeoutException;
  30 
  31 /**
  32  * Thread which catches exceptions thrown during the execution
  33  * and stores them for later analysis.
  34  *
  35  * <pre>
  36  * {@code
  37  * TestThread thread = new TestThread(new XRun() {
  38  *      public void run() {
  39  *      // do something
  40  *      }
  41  * });
  42  * thread.start();
  43  * // do something
  44  * Throwable uncaught = thread.getUncaught();
  45  * }
  46  * </pre>
  47  */
  48 public class TestThread extends Thread {


 187     }
 188 
 189     /**
 190      * Waits during {@code timeout} for {@link TestThread} to die
 191      * and returns exception caught during the execution.
 192      *
 193      * @param timeout The time to wait in milliseconds
 194      * @return Exception caught during the execution
 195      * @throws InterruptedException
 196      */
 197     public Throwable joinAndReturn(long timeout) throws InterruptedException {
 198         join(timeout);
 199         if (isAlive()) {
 200             return new TimeoutException();
 201         }
 202         if (uncaught != null) {
 203             return uncaught;
 204         }
 205         return null;
 206     }
 207 
 208     /**
 209      * Waits until {@link TestThread} is in the certain {@link State}
 210      * and blocking on {@code object}.
 211      *
 212      * @param state The thread state
 213      * @param object The object to block on
 214      */
 215     public void waitUntilBlockingOnObject(Thread.State state, Object object) {
 216         String want = object == null ? null : object.getClass().getName() + '@'
 217                 + Integer.toHexString(System.identityHashCode(object));
 218         ThreadMXBean tmx = ManagementFactory.getThreadMXBean();
 219         while (isAlive()) {
 220             ThreadInfo ti = tmx.getThreadInfo(getId());
 221             if (ti.getThreadState() == state
 222                     && (want == null || want.equals(ti.getLockName()))) {
 223                 return;
 224             }
 225             try {
 226                 Thread.sleep(1);
 227             } catch (InterruptedException e) {
 228             }
 229         }
 230     }
 231 
 232     /**
 233      * Waits until {@link TestThread} is in native.
 234      */
 235     public void waitUntilInNative() {
 236         ThreadMXBean tmx = ManagementFactory.getThreadMXBean();
 237         while (isAlive()) {
 238             ThreadInfo ti = tmx.getThreadInfo(getId());
 239             if (ti.isInNative()) {
 240                 return;
 241             }
 242             try {
 243                 Thread.sleep(1);
 244             } catch (InterruptedException e) {
 245             }
 246         }
 247     }
 248 
 249 }


   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 package jdk.testlibrary;
  25 



  26 import java.util.concurrent.TimeoutException;
  27 
  28 /**
  29  * Thread which catches exceptions thrown during the execution
  30  * and stores them for later analysis.
  31  *
  32  * <pre>
  33  * {@code
  34  * TestThread thread = new TestThread(new XRun() {
  35  *      public void run() {
  36  *      // do something
  37  *      }
  38  * });
  39  * thread.start();
  40  * // do something
  41  * Throwable uncaught = thread.getUncaught();
  42  * }
  43  * </pre>
  44  */
  45 public class TestThread extends Thread {


 184     }
 185 
 186     /**
 187      * Waits during {@code timeout} for {@link TestThread} to die
 188      * and returns exception caught during the execution.
 189      *
 190      * @param timeout The time to wait in milliseconds
 191      * @return Exception caught during the execution
 192      * @throws InterruptedException
 193      */
 194     public Throwable joinAndReturn(long timeout) throws InterruptedException {
 195         join(timeout);
 196         if (isAlive()) {
 197             return new TimeoutException();
 198         }
 199         if (uncaught != null) {
 200             return uncaught;
 201         }
 202         return null;
 203     }










































 204 }
< prev index next >