view src/EDU/oswego/cs/dl/util/concurrent/TimedCallable.java @ 27:511648fa4d64 Version 2.1

Version to 2.1
author Dan Creswell <dan.creswell@gmail.com>
date Mon, 04 Jan 2010 13:00:40 +0000
parents 3dc0c5604566
children
line wrap: on
line source

/*
  TimedCallable.java
  
  Originally written by Joseph Bowbeer and released into the public domain.
  This may be used for any purposes whatsoever without acknowledgment.
 
  Originally part of jozart.swingutils.
  Adapted by Doug Lea for util.concurrent.

  History:
  Date       Who                What
  11dec1999   dl                Adapted for util.concurrent

 */

package EDU.oswego.cs.dl.util.concurrent;

/**
 * TimedCallable runs a Callable function for a given length of time.
 * The function is run in its own thread. If the function completes
 * in time, its result is returned; otherwise the thread is interrupted
 * and an InterruptedException is thrown.
 * <p>
 * Note: TimedCallable will always return within the given time limit
 * (modulo timer inaccuracies), but whether or not the worker thread
 * stops in a timely fashion depends on the interrupt handling in the
 * Callable function's implementation. 
 *
 * @author  Joseph Bowbeer
 * @version 1.0
 *
 * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]

 */

public class TimedCallable extends ThreadFactoryUser implements Callable {

  private final Callable function;
  private final long millis;
  
  public TimedCallable(Callable function, long millis) {
    this.function = function;
    this.millis = millis;
  }
  
  public Object call() throws Exception {
    
    FutureResult result = new FutureResult();

    Thread thread = getThreadFactory().newThread(result.setter(function));
   
    thread.start();
    
    try {
      return result.timedGet(millis);
    }
    catch (InterruptedException ex) {
      /* Stop thread if we were interrupted or timed-out
         while waiting for the result. */
      thread.interrupt();
      throw ex;
    }
  }
}