ThreadInterruptObserver implements IThreadInterruptObservable { @Override public void addInterruptListener(final IThreadInterruptListener listener) { addInterruptListener(Thread.currentThread(), listener); } ThreadInterruptObserver(final String threadNamePrefix, final IExceptionHandler exceptionHandler);  ThreadInterruptObserver(final String threadNamePrefix, final IExceptionHandler exceptionHandler, final long period);  ThreadInterruptObserver(
        final ThreadFactory threadFactory,
        final IExceptionHandler exceptionHandler,
        final long period);  ThreadInterruptObserver(
        final ScheduledExecutorService executor,
        final IExceptionHandler exceptionHandler,
        final long period); synchronized void start(); synchronized void stop(); synchronized void shutdown(); boolean isRunning(); @Override long getDelayMillis(); @Override void addInterruptListener(final IThreadInterruptListener listener); @Override void addInterruptListener(final Thread thread, final IThreadInterruptListener listener); @Override boolean removeInterruptListener(final IThreadInterruptListener listener); @Override boolean removeInterruptListener(final Thread thread, final IThreadInterruptListener listener); boolean isCurrentThreadObserved(); boolean isThreadObserved(final Thread thread);  }
@Test public void testHandleExceptions() { final RuntimeException expectedException = new RuntimeException("Foo has happended"); final IThreadInterruptListener listener1 = new IThreadInterruptListener() { @Override public void interrupted(final Thread thread) { throw expectedException; } }; final IThreadInterruptListener listener2 = Mockito.mock(IThreadInterruptListener.class); observer.addInterruptListener(listener1); observer.addInterruptListener(listener2); int interruptedCount = 0; for (int i = 0; i < 5; i++) { Thread.currentThread().interrupt(); interruptedCount++; executor.executeEvents(); Mockito.verify(exceptionHandler, Mockito.times(interruptedCount)).handleException(expectedException); Mockito.verify(listener2, Mockito.times(interruptedCount)).interrupted(Thread.currentThread()); Assert.assertTrue(Thread.interrupted()); Assert.assertFalse(Thread.currentThread().isInterrupted()); timeProvider.elapsedMillis(DEFAULT_PERIOD); } }