### Question:
Timeout implements AutoCloseable { public Manager set(double timeout) { final Task task = new Task(timeout); return new Manager() { @Override public void run(final Runnable failing) { executor.execute(new Runnable() { @Override public void run() { if (task.id >= 0L) { return; } task.failing = failing; task.id = nextId; nextId++; tasks.put(task.id, task); task.reset(); } }); } @Override public void reset() { executor.execute(new Runnable() { @Override public void run() { if (task.id < 0L) { return; } task.reset(); } }); } @Override public void cancel() { executor.execute(new Runnable() { @Override public void run() { if (task.id < 0L) { return; } tasks.remove(task.id); } }); } }; } Timeout(); @Override void close(); Manager set(double timeout);  }### Answer:
@Test public void test() throws Exception { try (Timeout timeout = new Timeout()) { final boolean[] failed = new boolean[] { false }; Runnable r = new Runnable() { @Override public void run() { LOGGER.debug("----"); } }; Timeout.Manager m = timeout.set(1d); m.run(new Runnable() { @Override public void run() { synchronized (failed) { failed[0] = true; } } }); for (int i = 0; i < 5; i++) { Thread.sleep(500); m.reset(); r.run(); } synchronized (failed) { Assertions.assertThat(failed[0]).isFalse(); } Thread.sleep(2000); synchronized (failed) { Assertions.assertThat(failed[0]).isTrue(); } } }