java - CompletableFuture.acceptEither -


i experimenting completablefuture api jdk8, trying out accepteither() method it. please have @ code below (and put forth concern):

public class accepteither {      public static void main(string[] args) {          completablefuture<double> completablefuture2          = completablefuture.supplyasync(tasksupplier::getsomearbitrarydouble);          completablefuture<double> completablefuture3          = completablefuture.supplyasync(tasksupplier::getanotherarbitrarydouble);          completablefuture<void>completablefutureforacpteither              = completablefuture2.accepteither(completablefuture3, (val)-> {                       system.out.println("val: "+val);                 });           /*while(true){             if(completablefutureforacpteither.isdone())                 break;             system.out.println("task did not complete");         }         system.out.println("task completed");*/          system.out.println("exiting main method");       }  }   class tasksupplier{     static double getsomearbitrarydouble(){         try {             thread.sleep(5000l);         } catch (interruptedexception e) {             e.printstacktrace();         }         return 5;     }     static double getanotherarbitrarydouble(){         try {             thread.sleep(3000l);         } catch (interruptedexception e) {             e.printstacktrace();         }         return 10;     } } 

please note commented infinite while loop! in situation output follows:

exiting main method 

we can see completablefuture returned accepteither() not execute supplied action.

however, if un-comment infinite while loop, following output:

task did not complete task did not complete task did not complete val: 10.0 task did not complete task did not complete task did not complete task did not complete task completed exiting main method 

following documentation of accepteither method:

public completablefuture<void> accepteither(completionstage<? extends t> other,                                             consumer<? super t> action)  description copied interface: completionstage returns new completionstage that, when either or other given stage complete normally, executed corresponding result argument supplied action. see completionstage documentation rules covering exceptional completion. 

quite noticeably time completablefuture returned accepteither() executed.

following these observations can see dependency between main-thread (running main method) , thread (from fork join common pool) execute action.

i feel main thread should not reach end of life, before pooled thread executes supplied action! not sure of deduction @ all. please enlighten me what's happening here.

the main thread doesn't wait tasks in fork join common pool. documentation of common pool says:

however pool , ongoing processing automatically terminated upon program system.exit(int). program relies on asynchronous task processing complete before program termination should invoke commonpool().awaitquiescence, before exit.

you can use own executor using other methods of completablefuture accept executor in signature. doing that, submitted tasks executed on executor.

executorservice executor = executors.newcachedthreadpool(); completablefuture.supplyasync( () -> {...}, executor); ... 

and shutdown executor @ end of main thread

executor.shutdown(); 

Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -