Watchdog implements Runnable { public Watchdog(String name, long timeout, TimeUnit unit) { this.name = name; if (timeout < 1) { throw new IllegalArgumentException(String.format("Invalid timeout: %d", timeout)); } this.timeout = unit.toMillis(timeout); } Watchdog(String name, long timeout, TimeUnit unit);  Watchdog(long timeout, TimeUnit unit); synchronized void start(Cancellable cancellable); synchronized void stop(); synchronized void run();  }
@Test public void testWatchDog() throws Exception { final CountDownLatch latch = new CountDownLatch(1); final boolean[] cancel = new boolean[]{false}; final Cancellable myCancellable = new Cancellable() { @Override public void cancel() throws Exception { cancel[0] = true; latch.countDown(); } }; final Watchdog watchdog = new Watchdog(1, TimeUnit.SECONDS); watchdog.start(myCancellable); latch.await(2, TimeUnit.SECONDS); Assert.assertTrue(cancel[0], "cancellation failed"); }
@Test public void testWatchDog() throws Exception { final CountDownLatch latch = new CountDownLatch(1); final boolean[] cancel = new boolean[] {false}; final Cancellable myCancellable = new Cancellable() { @Override public void cancel() throws Exception { cancel[0] = true; latch.countDown(); } }; final Watchdog watchdog = new Watchdog(1, TimeUnit.SECONDS); watchdog.start(myCancellable); latch.await(2, TimeUnit.SECONDS); Assert.assertTrue(cancel[0], "cancellation failed"); }