InterruptibleProxy implements DelegatingInvocationHandler { public static <T> T newProxyInstance(Class<T> interfaceClass, T delegate, CancelDelegate cancel) { return newProxyInstance(interfaceClass, delegate, cancel, defaultExecutor.get()); } private  InterruptibleProxy(Object delegate, CancelDelegate cancel, ExecutorService executor); static T newProxyInstance(Class<T> interfaceClass, T delegate, CancelDelegate cancel); @SuppressWarnings("unchecked") static T newProxyInstance(Class<T> interfaceClass, T delegate,
            CancelDelegate cancel, ExecutorService executor); @Override Object invoke(final Object proxy, final Method method, final Object[] args); @Override Object getDelegate();  }
@Test public void testInterrupt() throws InterruptedException, BrokenBarrierException { final CyclicBarrier barrier = new CyclicBarrier(2); final Thread callingThread = Thread.currentThread(); final AtomicBoolean gotInterrupted = new AtomicBoolean(false); List<String> strings = new ArrayList<String>() { private static final long serialVersionUID = 1L; @Override public String get(int arg0) { if (arg0 == 1) { try { Thread.sleep(Long.MAX_VALUE); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException(e); } } return super.get(arg0); } @Override public String remove(int arg0) { assertNotEquals(Thread.currentThread(), callingThread); while(true) { try { Thread.sleep(100); callingThread.interrupt(); } catch (InterruptedException e) { gotInterrupted.set(true); try { barrier.await(); } catch (InterruptedException e1) { Thread.currentThread().interrupt(); throw new RuntimeException(e1); } catch (BrokenBarrierException e1) { throw new RuntimeException(e1); } throw new PalantirRuntimeException(e); } } } }; strings.add("Foo"); @SuppressWarnings("unchecked") List<String> proxy = InterruptibleProxy.newProxyInstance(List.class, strings, CancelDelegate.CANCEL); assertEquals("Foo", proxy.get(0)); try { Thread.currentThread().interrupt(); assertEquals("Foo", proxy.get(1)); fail("Should be interrupted"); } catch (PalantirInterruptedException e) { } try { assertEquals("Foo", proxy.get(1000)); fail("Should throw exception"); } catch (IndexOutOfBoundsException e) { } try { proxy.remove(0); fail("Should be interrupted"); } catch (PalantirInterruptedException e) { } barrier.await(); assertTrue(gotInterrupted.get()); }