如何从Callable()返回对象

如何从Callable()返回对象,第1张

如何从Callable()返回对象

添加到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));        }    }


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5487117.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存