ScheduledSupplier implements Supplier<T>, AutoCloseable { @Override public void close() throws Exception { try { scheduler.shutdownNow(); scheduler.awaitTermination(10, SECONDS); } catch (Exception e) { log.warn("Exceptional shutdown of scheduler.", e); throw e; } if (delegate instanceof AutoCloseable) { ((AutoCloseable) delegate).close(); } } ScheduledSupplier(@NonNull Supplier<T> delegate, long interval, @NonNull TimeUnit timeUnit); @Override T get(); @Override void close();  }
@Test public void checkClose() throws Exception { ScheduledSupplier<String> supplier = new ScheduledSupplier<>(delegate, executor, 1, SECONDS); supplier.close(); verify(executor).shutdownNow(); verify(executor).awaitTermination(10, SECONDS); }