2011-04-01 93 views
7

我试图从call()返回一个2d数组,我遇到了一些问题。到目前为止我的代码是:如何从Callable返回对象()

//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;         
    } 

此代码编译,这不是给我回阵列。我确定我可能使用了错误的语法,但是我找不到任何好的例子。

编辑:改变了它有点

+0

它是什么回来?长度从哪里来? Array1和Array2从哪里来? – 2011-04-01 16:50:32

+0

我很确定'answer = multiplyArray(Array1,Array2,length,);''因为有一个额外的''''不会编译。你应该发布更多的代码,因为我不认为有可能在没有猜测的情况下做出假设。 – 2011-04-01 16:52:40

+0

什么是调用call()方法? – dontocsata 2011-04-01 16:53:29

回答

5

添加到约瑟夫·奥廷格的回答,通过赎回的号召内使用()方法,你可以使用闭值:

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)); 
     } 
    } 
+0

我必须对Joseph Ottinger和Marimuthu Madasamy都表示非常感谢。我最终在你的帮助下到达了那里。 – user650309 2011-04-01 19:30:47

9

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

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])); 
    } 
} 
} 

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

发生的下一件事是result.get(),它将阻塞直到可调用函数返回。

你可能不应该自己做线程管理。

+0

这非常有用,但是如何将对象发送到可调用对象?我需要发送两个数组。 – user650309 2011-04-01 17:04:02

+0

您可以在构建时传递它们,或者扩展Callable接口以使用实例本地数组。 – 2011-04-01 17:30:09

+0

很好的回答,以及促进ExecutorService使用情况的荣誉。真的没有太多的地方应该再次使用线程。 – 2011-04-01 18:13:41

2

除了Joseph的优秀答案,请注意您的方法签名是int[][] call(int[][])。如果你参考了Callablejavadoc,你会发现Callablecall()方法没有任何参数。所以你的方法是一个重载,而不是一个重载,所以不会被调用Callablecall()方法的任何东西调用。

+0

嘿,谢谢。我认为只是显示正确的用法更容易,而不是试图找出(略微不完整的)示例代码中实际发生的事情。 – 2011-04-01 17:03:19

+0

正如德国人所说的那样:“这个问题在哪里”。 – 2014-11-07 09:28:00