Java 如何将 Callable 与 void 返回类型一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22795563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:56:08  来源:igfitidea点击:

How to use Callable with void return type?

javamultithreadingcallable

提问by AKIWEB

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 -

假设我的第一个界面是 -

public Interface interfaceA {
    public void abc() throws Exception;
}

And its implementation is -

它的实现是——

public class TestA implements interfaceA {

// abc method
}

I am calling it like this -

我是这样称呼它的——

TestA testA = new TestA();
testA.abc();

Now my second interface is -

现在我的第二个界面是 -

public Interface interfaceB {
    public void xyz() throws Exception;
}

And its implementation is -

它的实现是——

public class TestB implements interfaceB {

// xyz method   
}

I am calling it like this -

我是这样称呼它的——

TestB testB = new TestB();
testB.xyz();

Problem Statement:-

问题陈述:-

Now my question is - Is there any way, I can execute these two implementation classes in parallel? I don't want to run it in sequential.

现在我的问题是 - 有什么办法可以并行执行这两个实现类吗?我不想按顺序运行它。

Meaning, I want to run TestAand TestBimplementation in parallel? Is this possible to do?

意思是,我想并行运行TestATestB实施?这是可能的吗?

I was thinking to use Callable here but not sure how to use Callable with void return type here -

我想在这里使用 Callable 但不确定如何在此处使用带有 void 返回类型的 Callable -

Let's use TestB class as an example:

我们以 TestB 类为例:

public interface interfaceB {
    public void xyz() throws Exception;
}

public class TestB implements interfaceB, Callable<?>{

    @Override
    public void xyz() throws Exception
    {
        //do something

    }

    @Override
    public void call() throws Exception
    {
        xyz();
    }
}

Above code gives compilation error..

上面的代码给出了编译错误..

UPDATE:-

更新:-

It looks like lot of people are suggesting to use Runnable instead of callable. But not sure how do I use Runnable here so that I can execute TestA and TestBin parallel.

看起来很多人都建议使用 Runnable 而不是 callable。但不确定如何在这里使用 Runnable 以便我可以TestA and TestB并行执行。

采纳答案by nosid

You can use java.lang.Threadfor parallel execution. However, in most cases it's easier to use an java.util.concurrent.ExecutorService. The latter provides a method to submit a Callableand returns a Futureto get the result later (or wait for completion).

您可以使用java.lang.Thread进行并行执行。但是,在大多数情况下,使用java.util.concurrent.ExecutorService更容易。后者提供了提交Callable并返回Future以稍后获取结果(或等待完成)的方法。

If testA.abc()and testB.xyz()should be executed in parallel, you use the ExecutorServiceto execute the former in a separate thread whereas the latter is executed in the original thread. Then you wait for the completion of the former for synchronization.

如果testA.abc()testB.xyz()应该并行执行,则使用ExecutorService在单独的线程中执行前者,而后者在原始线程中执行。然后等待前者完成同步。

ExecutorService executor = ... // e.g. Executors.newFixedThreadPool(4);

Future<Void> future = executor.submit(new Callable<Void>() {
    public Void call() throws Exception {
        testA.abc();
        return null;
    }
});
testB.xyz();
future.get(); // wait for completion of testA.abc()

回答by Rogue

Why would you need a void for running something in Parallel? For one, if you don't need the return value, you can simply return null.

为什么你需要一个空白来并行运行某些东西?一方面,如果您不需要返回值,您可以简单地 return null

To make something parallel you need to use threading/scheduling. I would personally recommend avoiding Callables, and using Runnables instead (and hey, no return value).

要使某些事情并行,您需要使用线程/调度。我个人建议避免使用 Callables,而是使用 Runnables(嘿,没有返回值)。

回答by AddJ

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/

另外,如果您在 Spring 环境中,则可以使用:https: //spring.io/guides/gs/async-method/