java library which enables composing futures (flatMap) asynchronously -
i thought new java 8 completablefuture able execute async tasks, 1 after without blocking threads, using "thencomposeasync" method.
i think not case. thread waits synchronously task completed (completablefuture.java:616 r = fr.waitingget(false);)
and following piece of code never executes "task 3" if nthreads <= 2:
int nthreads= 2; executor e= executors.newfixedthreadpool(nthreads); completablefuture.runasync( () -> { system.out.println("task 1 threadid " + thread.currentthread().getid()); }, e).thencomposeasync( (void v1) -> { return completablefuture.runasync( () -> { system.out.println("task 2 threadid " + thread.currentthread().getid()); }, e).thencomposeasync((void v2) -> { return completablefuture.runasync( () -> { system.out.println("task 3 threadid " + thread.currentthread().getid()); }, e); }, e); }, e).join(); system.out.println("finished");
¿what other java library should use? want code execute in 1 thread (with nthreads = 1).
this 1 working. instead of completablefuture.thencomposeasync, call:
static public <t,u> completablefuture<u> thencomposeasyncnowait(completablefuture<t> first, function<? super t, ? extends completablefuture<u>> fn, executor executor) { completablefuture<u> ret= new completablefuture<>(); first.handleasync( (t t, throwable e) -> { if ( e != null ) ret.completeexceptionally(e); else fn.apply(t).handleasync( (u u, throwable e2) -> { if ( e2 != null ) ret.completeexceptionally(e2); else ret.complete(u); return null; }, executor); return null; }, executor); return ret; }
Comments
Post a Comment