Class ThreadCleaner


public final class ThreadCleaner extends Object
Cleans up threads if some object has been garbage collected.

Sometimes it is necessary to create a thread that delivers data to a synchronous consumer. This thread keeps the consumer running until the consumer expects no more input and thus terminates the thread.

Due to an error condition it may happen, however, that the terminating event never occurs and the thread runs forever. As a possible remedy, this class allows the thread to be associated with the lifetime of an arbitrary object. When the object is garbage collected, the thread is terminated automatically.

  • Method Details

    • watch

      public static void watch(Object referent, Thread thread)
      Watch the referent and terminate the thread if it is garbage collected.
      referent - the referent
      thread - the thread