Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return object from Callable()

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

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

like image 624
user650309 Avatar asked Apr 01 '11 16:04

user650309


People also ask

How can I get my Callable result?

submit() method returns immediately and gives you a Future. Once you have obtained a future, you can execute other tasks in parallel while your submitted task is executing, and then use future. get() method to retrieve the result of the future.

What is the return type of Callable call method?

The return type of the call() method of the interface is an Object. Hence, the call() method returns an Object.

What does Callable interface return?

Interface Callable<V> A task that returns a result and may throw an exception. Implementors define a single method with no arguments called call. The Callable interface is similar to Runnable , in that both are designed for classes whose instances are potentially executed by another thread.

How do you execute a Callable in Java?

For supporting this feature, the Callable interface is present in Java. For implementing Runnable, the run() method needs to be implemented which does not return anything, while for a Callable, the call() method needs to be implemented which returns a result on completion.


2 Answers

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

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.

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

You probably shouldn't do the Thread management yourself.

like image 168
Joseph Ottinger Avatar answered Oct 05 '22 19:10

Joseph Ottinger


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

    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));
        }
    }
like image 37
Marimuthu Madasamy Avatar answered Oct 05 '22 18:10

Marimuthu Madasamy