java 如何从 Callable() 返回对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5516383/
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-10-30 11:30:06  来源:igfitidea点击:

How to return object from Callable()

javamultithreadingcallable

提问by user650309

I'm trying to return a 2d array from call(), I'm having some issues. My code so far is:

我正在尝试从 call() 返回一个二维数组,我遇到了一些问题。到目前为止我的代码是:

//this is the end of main   
Thread t1 = new Thread(new ArrayMultiplication(Array1, Array2, length));
t1.start(); 
}

    public int[][] call(int[][] answer)
    {       

    int[][] answer = new int[length][length]; 

    answer = multiplyArray(Array1, Array2, length); //off to another function which returns the answer to here  

    return answer;                                  
    }

This code compiles, this is not returning my array. I'm sure I'm probably using the wrong syntax, but I can't find any good examples.

此代码编译,这不会返回我的数组。我确定我可能使用了错误的语法,但我找不到任何好的例子。

EDIT: changed it a bit

编辑:稍微改变一下

采纳答案by Marimuthu Madasamy

Adding to Joseph Ottinger's answer, to pass values to be used inside Callable's call() method, you can use closures:

添加到 Joseph Ottinger 的答案中,要传递要在 Callable 的 call() 方法中使用的值,您可以使用闭包:

    public static Callable<Integer[][]> getMultiplierCallable(final int[][] xs,
            final int[][] ys, final int length) {
        return new Callable<Integer[][]>() {
            public Integer[][] call() throws Exception {
                Integer[][] answer = new Integer[length][length];
                answer = multiplyArray(xs, ys, length);
                return answer;
            }
        };
    }

    public static void main(final String[] args) throws ExecutionException,
            InterruptedException {
        final int[][] xs = {{1, 2}, {3, 4}};
        final int[][] ys = {{1, 2}, {3, 4}};
        final Callable<Integer[][]> callable = getMultiplierCallable(xs, ys, 2);
        final ExecutorService service = Executors.newFixedThreadPool(2);
        final Future<Integer[][]> result = service.submit(callable);
        final Integer[][] intArray = result.get();
        for (final Integer[] element : intArray) {
            System.out.println(Arrays.toString(element));
        }
    }

回答by Joseph Ottinger

Here's some code demonstrating use of the Callable<> interface:

下面是一些演示 Callable<> 接口使用的代码:

public class test {
public static void main(String[] args) throws ExecutionException, InterruptedException {
    Callable callable = new Callable() {
        @Override
        public int[][] call() throws Exception {
            int[][] array = new int[5][];
            for (int i = 0; i < array.length; i++) {
                array[i] = new int[]{5 * i, 5 * i + 1, 5 * i + 2, 5 * i + 3};
            }

            return array;
        }
    };

    ExecutorService service = Executors.newFixedThreadPool(2);
    Future<int[][]> result = service.submit(callable);

    int[][] intArray = result.get();
    for (int i = 0; i < intArray.length; i++) {
        System.out.println(Arrays.toString(intArray[i]));
    }
}
}

What this does is construct an object that can be submitted to an executor service. It's fundamentally the same as a Runnable, except that it can return a value; what we're doing here is creating an ExecutorService with two threads, then submitting this callable to the service.

这样做是构造一个可以提交给执行程序服务的对象。它与 Runnable 基本相同,只是它可以返回一个值;我们在这里做的是创建一个带有两个线程的 ExecutorService,然后将这个可调用对象提交给服务。

The next thing that happens is the result.get(), which will block until the callable returns.

接下来发生的事情是 result.get(),它将阻塞直到可调用对象返回。

You probably shouldn't do the Thread management yourself.

您可能不应该自己进行线程管理。

回答by QuantumMechanic

In addition to Joseph's excellent answer, note that your method signature is int[][] call(int[][]). If you reference the Callablejavadoc you'll see that the Callable's call()method does not take any arguments. So your method is an overload, not an override, and so won't be called by anything that is calling Callable's call()method.

除了约瑟夫的出色回答,请注意您的方法签名是int[][] call(int[][]). 如果您参考Callablejavadoc,您将看到Callable'scall()方法不接受任何参数。所以你的方法是一个重载,而不是一个覆盖,所以不会被任何正在调用Callablecall()方法调用。