com.google.testing.threadtester
Class ThreadMonitor

java.lang.Object
  extended by com.google.testing.threadtester.ThreadMonitor

public class ThreadMonitor
extends Object

Utility class that monitors two threads, and waits for one of them to finish. If it determines that the first thread will not finish because of a monitor lock held by the second thread, it will return a status code to indicate that fact.

Author:
alasdair.mackintosh@gmail.com (Alasdair Mackintosh)

Constructor Summary
ThreadMonitor(Thread toWait, Thread other)
          Creates a new ThreadMonitor for the given threads.
 
Method Summary
static long getBlockerId(Thread thread)
          Returns the thread id of the thread that is blocking the input thread.
 boolean waitForThread()
          Waits for the first thread to terminate.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThreadMonitor

public ThreadMonitor(Thread toWait,
                     Thread other)
Creates a new ThreadMonitor for the given threads.

Parameters:
toWait - the thread to wait for.
other - the other thread that may hold locks that will prevent the first thread from completing.
Method Detail

getBlockerId

public static long getBlockerId(Thread thread)
Returns the thread id of the thread that is blocking the input thread. If the input thread is not blocked, returns -1.


waitForThread

public boolean waitForThread()
                      throws InterruptedException,
                             TestTimeoutException
Waits for the first thread to terminate. Returns true if it ran to completion, and false because of a synchronized lock held by the second thread.

Throws:
TestTimeoutException - if the thread was not blocked by the seond thread, but still failed to finish.
InterruptedException


Copyright © 2013. All Rights Reserved.