All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.cobraparser.util.JoinableTask Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package org.cobraparser.util;

/**
 * A task that can be used in a thread or thread pool. The caller can wait for
 * the task to finish by joining it.
 */
public abstract class JoinableTask implements SimpleThreadPoolTask {
  private boolean done = false;

  public final void run() {
    try {
      this.execute();
    } finally {
      synchronized (this) {
        this.done = true;
        this.notifyAll();
      }
    }
  }

  public final void forceDone() {
    synchronized (this) {
      this.done = true;
      this.notifyAll();
    }
  }

  public void join() throws InterruptedException {
    synchronized (this) {
      while (!this.done) {
        this.wait();
      }
    }
  }

  public void cancel() {
    this.forceDone();
  }

  protected abstract void execute();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy