@Override public V run() throws SuspendExecution, InterruptedException { try { return callable.call(); } catch (Exception e) { throw Exceptions.rethrow(e); } } };
static public RuntimeException sneakyThrow(Throwable t) { // http://www.mail-archive.com/javaposse@googlegroups.com/msg05984.html if (t == null) throw new NullPointerException(); Exceptions.<RuntimeException>sneakyThrow0(t); return null; }
/** * Blocks on the input fibers and creates a new list from the results. The result list is the same order as the * input list. * * @param fibers to combine */ public static <V> List<V> get(final List<Fiber<V>> fibers) throws InterruptedException { final List<V> results = new ArrayList<>(fibers.size()); //TODO on interrupt, should all input fibers be canceled? for (final Fiber<V> f : fibers) { try { results.add(f.get()); } catch (ExecutionException e) { throw Exceptions.rethrowUnwrap(e); } } return Collections.unmodifiableList(results); }
@Override public void uncaughtException(Strand s, Throwable e) { System.err.print("Exception in Fiber \"" + s.getName() + "\" "); if (e instanceof NullPointerException || e instanceof ClassCastException || Exceptions.unwrap(e) instanceof NullPointerException || Exceptions.unwrap(e) instanceof ClassCastException) System.err.println("If this exception looks strange, perhaps you've forgotten to instrument a blocking method. Run your program with -Dco.paralleluniverse.fibers.verifyInstrumentation to catch the culprit!"); System.err.println(e); Strand.printStackTrace(threadToFiberStack(e.getStackTrace()), System.err); checkInstrumentation(ExtendedStackTrace.of(e), true); } };
/** * Blocks on the input fibers and creates a new list from the results. The result list is the same order as the * input list. * * @param timeout to wait for all requests to complete * @param unit the time is in * @param fibers to combine */ public static <V> List<V> get(long timeout, TimeUnit unit, List<Fiber<V>> fibers) throws InterruptedException, TimeoutException { if (unit == null) return get(fibers); if (timeout < 0) timeout = 0; final List<V> results = new ArrayList<>(fibers.size()); long left = unit.toNanos(timeout); final long deadline = System.nanoTime() + left; //TODO on interrupt, should all input fibers be canceled? try { for (final Fiber<V> f : fibers) { if (left >= 0) { results.add(f.get(left, TimeUnit.NANOSECONDS)); left = deadline - System.nanoTime(); } else throw new TimeoutException("timed out sequencing fiber results"); } return Collections.unmodifiableList(results); } catch (ExecutionException e) { throw Exceptions.rethrowUnwrap(e); } }
private int getStackTraceDepth0() { try { return (Integer) getStackTraceDepth.invoke(t); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (InvocationTargetException e) { throw rethrow(e); } }
@Override public CloseableHttpResponse execute(final long nanoTime, final HttpRequestBase request) throws SuspendExecution, InterruptedException { // TODO See if timeout can be configured per-request final CloseableHttpResponse ret; try { ret = new Fiber<>((SuspendableCallable<CloseableHttpResponse>) () -> { try { return client.execute(request); } catch (final IOException e) { throw Exceptions.rethrowUnwrap(e); } }).start().get(); } catch (final ExecutionException e) { throw Exceptions.rethrowUnwrap(e); } if (validator != null) { validator.validate(ret); } return ret; }
static public RuntimeException sneakyThrow(Throwable t) { // http://www.mail-archive.com/javaposse@googlegroups.com/msg05984.html if (t == null) throw new NullPointerException(); Exceptions.<RuntimeException>sneakyThrow0(t); return null; }
private StackTraceElement getStackTraceElement0(int i) { try { return (StackTraceElement) getStackTraceElement.invoke(t, i); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (InvocationTargetException e) { throw rethrow(e); } }
@Override public void run() { try { callable.run(); } catch (SuspendExecution ex) { throw new AssertionError(ex); } catch (InterruptedException ex) { } catch (Exception e) { throw Exceptions.rethrow(e); } } }
protected void onException(Throwable t) { record("doExec", "exception in %s - %s, %s", this, t, t.getStackTrace()); throw Exceptions.rethrow(t); }
protected void onException(Throwable t) { if (future != null) future.setException(t); else throw Exceptions.rethrow(t); }
public static <X1 extends Throwable, X2 extends Throwable> RuntimeException rethrowUnwrap(Throwable t, Class<X1> exceptionClass1, Class<X2> exceptionClass2) throws X1, X2 { Throwable t1 = unwrap(t); if (exceptionClass1.isInstance(t1)) throw exceptionClass1.cast(t1); if (exceptionClass2.isInstance(t1)) throw exceptionClass2.cast(t1); throw rethrow(t1); }
@Override public void run() { try { runnable.run(); } catch (SuspendExecution ex) { throw new AssertionError(ex); } catch (InterruptedException ex) { } catch (Exception e) { throw Exceptions.rethrow(e); } } }
public static <X1 extends Throwable, X2 extends Throwable> RuntimeException rethrowUnwrap(Throwable t, Class<X1> exceptionClass1, Class<X2> exceptionClass2) throws X1, X2 { Throwable t1 = unwrap(t); if (exceptionClass1.isInstance(t1)) throw exceptionClass1.cast(t1); if (exceptionClass2.isInstance(t1)) throw exceptionClass2.cast(t1); throw rethrow(t1); }