### Question:
FiberAsync implements java.io.Serializable { public static <V, E extends Exception> V runBlocking(final ExecutorService exec, final CheckedCallable<V, E> callable) throws E, SuspendExecution, InterruptedException { return new ThreadBlockingFiberAsync<>(exec, callable).run(); } FiberAsync();  FiberAsync(boolean immediateExec); @SuppressWarnings("empty-statement") V run(); @SuppressWarnings("empty-statement") V run(final long timeout, final TimeUnit unit); V run(Timeout timeout); final boolean isCompleted(); final V getResult(); static V runBlocking(final ExecutorService exec, final CheckedCallable<V, E> callable); static V runBlocking(final ExecutorService exec, long timeout, TimeUnit unit, final CheckedCallable<V, E> callable); static V runBlocking(final ExecutorService exec, Timeout timeout, final CheckedCallable<V, E> callable);  }### Answer:
@Test public void whenCancelRunBlockingInterruptExecutingThread() throws Exception { final AtomicBoolean started = new AtomicBoolean(); final AtomicBoolean interrupted = new AtomicBoolean(); Fiber fiber = new Fiber(new SuspendableRunnable() { @Override public void run() throws SuspendExecution, InterruptedException { FiberAsync.runBlocking(Executors.newSingleThreadExecutor(), new CheckedCallable<Void, RuntimeException>() { @Override public Void call() throws RuntimeException { started.set(true); try { Thread.sleep(1000); } catch (InterruptedException e) { interrupted.set(true); } return null; } }); } }); fiber.start(); Thread.sleep(100); fiber.cancel(true); try { fiber.join(5, TimeUnit.MILLISECONDS); fail("InterruptedException not thrown"); } catch(ExecutionException e) { if (!(e.getCause() instanceof InterruptedException)) fail("InterruptedException not thrown"); } Thread.sleep(100); assertThat(started.get(), is(true)); assertThat(interrupted.get(), is(true)); }
@Test public void testRunBlocking() throws Exception { final Fiber fiber = new Fiber(new SuspendableRunnable() { @Override public void run() throws SuspendExecution, InterruptedException { String res = FiberAsync.runBlocking(Executors.newCachedThreadPool(), new CheckedCallable<String, InterruptedException>() { public String call() throws InterruptedException { Thread.sleep(300); return "ok"; } }); assertThat(res, equalTo("ok")); } }).start(); fiber.join(); }
@Test public void testRunBlockingWithTimeout1() throws Exception { final Fiber fiber = new Fiber(new SuspendableRunnable() { @Override public void run() throws SuspendExecution, InterruptedException { try { String res = FiberAsync.runBlocking(Executors.newCachedThreadPool(), 400, TimeUnit.MILLISECONDS, new CheckedCallable<String, InterruptedException>() { public String call() throws InterruptedException { Thread.sleep(300); return "ok"; } }); assertThat(res, equalTo("ok")); } catch (TimeoutException e) { fail(); } } }).start(); fiber.join(); }
@Test public void testRunBlockingWithTimeout2() throws Exception { final Fiber fiber = new Fiber(new SuspendableRunnable() { @Override public void run() throws SuspendExecution, InterruptedException { try { String res = FiberAsync.runBlocking(Executors.newCachedThreadPool(), 100, TimeUnit.MILLISECONDS, new CheckedCallable<String, InterruptedException>() { public String call() throws InterruptedException { Thread.sleep(300); return "ok"; } }); fail(); } catch (TimeoutException e) { } } }).start(); fiber.join(); }