// ************************************************************************ // $Id: Timed.java 596 2005-07-22 05:55:12Z mdeters $ // ************************************************************************ // // jRate // // Copyright (C) 2001-2005 by Angelo Corsaro. // // All Rights Reserved. // // Permission to use, copy, modify, and distribute this software and // its documentation for any purpose is hereby granted without fee, // provided that the above copyright notice appear in all copies and // that both that copyright notice and this permission notice appear // in supporting documentation. I don't make any representations // about the suitability of this software for any purpose. It is // provided "as is" without express or implied warranty. // // // ************************************************************************* // // ************************************************************************* package javax.realtime; /** * Perform a computation (or subcomputation) that will be interrupted * after a specified duration. * * @author Morgan Deters * @version 1.0 * @since 0.3.8 */ public class Timed extends AsynchronouslyInterruptedException { private HighResolutionTime time; /** * Constructs a new {@link Timed}. * * @param time the timeout for this {@link Timed} instance */ public Timed(HighResolutionTime time) { this.time = time; } /** * Start the timer, perform the computation (or subcomputation) * logic, and interrupt it (if it hasn't finished) when the timer * runs out. * * @param logic the timed computation to perform * @return true if the computation is interrupted; false if it * finishes normally */ public boolean doInterruptible(Interruptible logic) { final RealtimeThread rtt = RealtimeThread.currentRealtimeThread(); AsyncEventHandler aeh = new AsyncEventHandler(new Runnable() { public void run() { rtt.interrupt(); } }); OneShotTimer ost = new OneShotTimer(time, aeh); ost.start(); try { logic.run(this); return true; } catch(AsynchronouslyInterruptedException aie) { return false; } } /** * Resets the timeout for the next timed computation. * * @param time the new timeout for this {@link Timed} instance */ public void resetTime(HighResolutionTime time) { this.time = time; } }