java - Variable callables with different return types -


the problem have in hand methods one(), two(), three(), four() have different return types say, a, b, c, d , need spawn variable numbers of threads (one each method depending on use case. means want call subset of methods @ time.) now, using cachedthreadpool submit these callables. code below:

public class dispatcher {    public void dispatch(list<methodnames> methodnames) {     //now going iterate through list of methodnames     //and submit each method `executorservice`     for(methodnames m : methodnames) {       switch(m) {         case one: //submit one()                   //wait , future                   future<a> future = cachepool.submit(new callable<a>() {                     @override                     public call() {                       return one();                     });                   response = future.get();               break;         ....       }     }   } }  public enum methodnames {   one, two, three, 4 }  //example methods: public one() { }  public b two() { } 

my question how above such method calls made without having wait 1 finish. also, how gather futures , wait them finish cause futures have different generic type future<a>, future<b> etc. make call submit() inside case statement don't have access returned future<t> outside case. if, else instead of for loop trying figure out if there better way achieve this.

i way -

  • create interface, let's i.
  • have classes a, b, c , d implements i.
  • use enums valueof , object overriding remove case statement.
  • use polymorphism , return i methods.
  • below code (not including a, b, c, d, i) plain class , interface - not doing much.

below code:

dispatcher.java

package com.test.thread;  import java.util.hashmap; import java.util.map; import java.util.concurrent.executionexception;  public class dispatcher {  public void dispatch() throws interruptedexception, executionexception {     map<methodnames, future<i>> reponse = new hashmap<methodnames, future<i>>();     executorservice cachepool = executors.newcachedthreadpool();     (methodnames methodnames : methodnames.values()) {         future<i> future = cachepool.submit(methodnames.worker());         reponse.put(methodnames, future);     }     cachepool.awaittermination(5, timeunit.minutes);     for(methodnames key : reponse.keyset()) {         result = reponse.get(key).get();         system.out.println("result :: " + result);     } }  public static void main(string[] args) throws interruptedexception, executionexception {     new dispatcher().dispatch(); } 

}

methodnames.java

package com.test.thread;  import java.util.concurrent.*;  public enum methodnames { 1 {     @override     public callable<i> worker() {         return new callable<i>() {             @override             public call() throws interruptedexception {                 system.out.println("thread1");                 timeunit.seconds.sleep(30);               return new a();             }};     } }, 2 {     @override     public callable<i> worker() throws interruptedexception {         return new callable<i>() {             @override             public call() throws interruptedexception {                 system.out.println("thread2");                 timeunit.seconds.sleep(30);               return new b();             }};     } }, 3 {     @override     public callable<i> worker() throws interruptedexception {         return new callable<i>() {             @override             public call() throws interruptedexception {                 system.out.println("thread3");                 timeunit.seconds.sleep(30);               return new c();             }};     } }, 4 {     @override     public callable<i> worker() throws interruptedexception {         return new callable<i>() {             @override             public call() throws interruptedexception {                 system.out.println("thread");                 timeunit.seconds.sleep(30);               return new d();             }};     } }; public abstract callable<i> worker() throws interruptedexception; 

}


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -