Clover coverage report - PLT Utilities Test Coverage (plt-20120304-r5436)
Coverage timestamp: Sat Mar 3 2012 22:01:56 CST
file stats: LOC: 97   Methods: 5
NCLOC: 27   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DelayedInterrupter.java 50% 100% 100% 94.1%
coverage coverage
 1    /*BEGIN_COPYRIGHT_BLOCK*
 2   
 3    PLT Utilities BSD License
 4   
 5    Copyright (c) 2007-2010 JavaPLT group at Rice University
 6    All rights reserved.
 7   
 8    Developed by: Java Programming Languages Team
 9    Rice University
 10    http://www.cs.rice.edu/~javaplt/
 11   
 12    Redistribution and use in source and binary forms, with or without modification, are permitted
 13    provided that the following conditions are met:
 14   
 15    - Redistributions of source code must retain the above copyright notice, this list of conditions
 16    and the following disclaimer.
 17    - Redistributions in binary form must reproduce the above copyright notice, this list of
 18    conditions and the following disclaimer in the documentation and/or other materials provided
 19    with the distribution.
 20    - Neither the name of the JavaPLT group, Rice University, nor the names of the library's
 21    contributors may be used to endorse or promote products derived from this software without
 22    specific prior written permission.
 23   
 24    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 25    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 26    FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND
 27    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 28    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 29    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 30    IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 31    OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 32   
 33    *END_COPYRIGHT_BLOCK*/
 34   
 35    package edu.rice.cs.plt.concurrent;
 36   
 37    import java.util.Timer;
 38    import java.util.TimerTask;
 39   
 40    import edu.rice.cs.plt.lambda.LazyThunk;
 41    import edu.rice.cs.plt.lambda.Thunk;
 42   
 43    /**
 44    * <p>Sets a "time bomb" on a specific thread: if the {@link #abort} method is not invoked within a specified
 45    * amount of time (in milliseconds), the thread will be interrupted. In contrast to {@link Object#wait(long)},
 46    * implementing a timeout in this way clearly distinguishes between a timeout-triggered wake-up and a
 47    * {@link Object#notify()}-triggered or spurious wake-up. This alternative is also useful where the thread to
 48    * timeout performs multiple blocking operations, invokes blocking APIs that don't support timeouts
 49    * (like {@link java.io.InputStream#read}), or polls for an interrupted state.</p>
 50    *
 51    * <p>The timeout is implemented with a single {@link Timer} for all DelayedInterrupter instances. This
 52    * timer is a daemon: an outstanding DelayedInterrupter will not prevent the program from terminating.</p>
 53    */
 54    public class DelayedInterrupter {
 55   
 56    /**
 57    * Delays instantiation of the timer thread until it's actually needed. (Code may reference this
 58    * class, for example, but never actually create a DelayedInterrupter object.)
 59    */
 60    private static final LazyThunk<Timer> TIMER = new LazyThunk<Timer>(new Thunk<Timer>() {
 61  4 public Timer value() { return new Timer("DelayedInterrupter Timer", true); }
 62    });
 63   
 64    private final Thread _worker;
 65    private final TimerTask _task;
 66   
 67    /**
 68    * Create an interrupter for the current thread.
 69    * @param timeToInterrupt Number of milliseconds to allow an abort before the thread will be interrupted.
 70    */
 71  13 public DelayedInterrupter(long timeToInterrupt) { this(Thread.currentThread(), timeToInterrupt); }
 72   
 73    /**
 74    * Create an interrupter for the specified thread.
 75    * @param timeToInterrupt Number of milliseconds to allow an abort before the thread will be interrupted.
 76    */
 77  13 public DelayedInterrupter(Thread worker, final long timeToInterrupt) {
 78  13 _worker = worker;
 79  13 _task = new TimerTask() {
 80  5 public void run() { _worker.interrupt(); }
 81    };
 82  13 TIMER.value().schedule(_task, timeToInterrupt);
 83    }
 84   
 85    /**
 86    * Abort the request to interrupt the thread. When called from the worker thread (this is the intended usage),
 87    * clears the interrupted status, in case the interrupt occurred but was not detected.
 88    */
 89  8 public void abort() {
 90  8 _task.cancel();
 91  8 TIMER.value().purge();
 92  8 if (Thread.currentThread() == _worker) {
 93  8 Thread.interrupted(); // clear the interrupted status, in case it occurred but wasn't detected
 94    }
 95    }
 96   
 97    }