Example #1
0
  public static void timedRun(final Runnable r, long timeout, TimeUnit unit)
      throws InterruptedException {
    class RethrowableTask implements Runnable {
      private volatile Throwable t;

      public void run() {
        try {
          r.run();
        } catch (Throwable t) {
          this.t = t;
        }
      }

      void rethrow() {
        if (t != null) throw launderThrowable(t);
      }
    }

    RethrowableTask task = new RethrowableTask();
    final Thread taskThread = new Thread(task);
    taskThread.start();
    cancelExec.schedule(
        new Runnable() {
          public void run() {
            taskThread.interrupt();
          }
        },
        timeout,
        unit);
    taskThread.join(unit.toMillis(timeout));
    task.rethrow();
  }
  public static void runTaskBySchedule(final Runnable runnable, long timeout, TimeUnit unit)
      throws Exception {
    task = new RethrowableTask(runnable);
    taskThread = new Thread(task);
    taskThread.start();

    interruptScheduledExecutor = Executors.newScheduledThreadPool(1);
    interruptScheduledExecutor.schedule(
        new Runnable() {
          public void run() {
            taskThread.interrupt();
          }
        },
        timeout,
        unit);
    taskThread.join(unit.toMillis(timeout));
    try {
      task.rethrow();
    } catch (Throwable throwable) {
      System.out.println(throwable.getMessage());
    }
  }