如何在Java8中使用Future和executter的invokeAll处理多个返回类型?

时间:2018-03-29 11:27:40

标签: multithreading java-8 executor futuretask

调用invokeall时如何在future.get中处理多个返回类型?

List<Future<List<Object>>> futureListe = executor.invokeAll(Classableobjekts); //Objekts list contain two objects of different types say Class1 and CLass2.

    futureListe.stream()
      .map(future -> {
          try {
              return future.get().get(0);
          }
          catch (InterruptedException | ExecutionException e) {
              throw new IllegalStateException(e);
          }
      }).forEach(d -> {
            if(d instanceof Class1) {   //So after response i want to check for particular response which is different for each other
                do this;
             } 
      });

    } catch (InterruptedException ex) {

    }

如果将来返回多种类型或者是否有更好的方法来处理这种情况,它是否是正确的处理方式?

0 个答案:

没有答案