Class CheckedThread

java.lang.Object
java.lang.Thread
org.apache.flink.core.testutils.CheckedThread
All Implemented Interfaces:
Runnable

public abstract class CheckedThread extends Thread
A thread that additionally catches exceptions and offers a joining method that re-throws the exceptions.

Rather than overriding Thread.run() (or supplying a Runnable), one needs to extends this class and implement the go() method. That method may throw exceptions.

Exception from the go() method are caught and re-thrown when joining this thread via the sync() method.

  • Constructor Details

    • CheckedThread

      public CheckedThread()
      Unnamed checked thread.
    • CheckedThread

      public CheckedThread(String name)
      Checked thread with a name.
      Parameters:
      name - the name of the new thread
      See Also:
  • Method Details

    • go

      public abstract void go() throws Exception
      This method needs to be overwritten to contain the main work logic. It takes the role of Thread.run(), but should propagate exceptions.
      Throws:
      Exception - The exceptions thrown here will be re-thrown in the sync() method.
    • run

      public final void run()
      This method is final - thread work should go into the go() method instead.
      Specified by:
      run in interface Runnable
      Overrides:
      run in class Thread
    • sync

      public void sync() throws Exception
      Waits until the thread is completed and checks whether any error occurred during the execution.

      This method blocks like Thread.join(), but performs an additional check for exceptions thrown from the go() method.

      Throws:
      Exception
    • sync

      public void sync(long timeout) throws Exception
      Waits with timeout until the thread is completed and checks whether any error occurred during the execution. In case of timeout an Exception is thrown.

      This method blocks like Thread.join(), but performs an additional check for exceptions thrown from the go() method.

      Throws:
      Exception
    • trySync

      public void trySync(long timeout) throws Exception
      Waits with timeout until the thread is completed and checks whether any error occurred during the execution.

      This method blocks like Thread.join(), but performs an additional check for exceptions thrown from the go() method.

      Throws:
      Exception