How to use Callable with void return type?

前端 未结 3 649
时光取名叫无心
时光取名叫无心 2021-02-02 05:21

I am working on a project in which I have multiple interface and two Implementations classes which needs to implement these two interfaces.

Suppose my first Interface is

3条回答
  •  孤独总比滥情好
    2021-02-02 05:56

    A shorter version:

    ExecutorService executor = ... // e.g. Executors.newFixedThreadPool(4);
    Future future = executor.submit(() -> testA.abc());
    testB.xyz();
    future.get(); // wait for completion of testA.abc()
    

    To be noted that having to run something in parallel with nothing to be returned could be a sign of a bad pattern :)

    Also, if you are in a Spring environment, you could use: https://spring.io/guides/gs/async-method/

提交回复
热议问题