Using callable to pass two parameters and returning an array of parameters

17,495

Solution 1

Change the signature of the class to

public class toArray implements Callable<String[]>

and the signature of the call method to

public String[] call() throws Exception

UPDATE: Ok, there are more than one error... See the following working sample

public class Test{

    public static void main(String[] args) {


        FutureTask<String[]> task = new FutureTask(new MyCallable("a", "b"));
        ExecutorService es = Executors.newSingleThreadExecutor();
        es.submit (task);
        try{
            String[] result = task.get();
            System.out.println(result[0] + result[1]);
        }
        catch(Exception e){
            System.err.println(e);
        }
        es.shutdown();
    }

    public static class MyCallable implements Callable<String[]>
    {

        private String string1, string2;

        public MyCallable (String first, String second)
        {
            string1= first;
            string2 = second;
        }

        @Override
        public String[] call() throws Exception {
            String [] allStrings = new String[2];
            allStrings[0] = string1;
            allStrings[1] = string2;
            return allStrings;
        }
    }
}

Solution 2

In Callable<V>, the parameterized type V stands for the result type of call(). Since you want to return a String[], instead of Callable<String>, use Callable<String[]>. Then, change the return type of call to String[]:

@Override
public String[] call() throws Exception {
    String [] allStrings = new String[2];
    allStrings[0] = string1;
    allStrings[1] = string2;
    return allStrings;
}

As a side note, class names in Java should be nouns, and should start with an uppercase letter.

Solution 3

You have not referenced your ToArray class in TheFunction, probably due to compilation errors (hint: fixes have already been mentioned).

Your new FutureTask will look like:

FutureTask<String[]> task = new FutureTask(new ToArray("foo", "bar"));

Without telling you the bare answer, you will hit 2 exceptions in the call() method in ToArray:

  • Firstly your String array is never initialised, but should be.
  • All arrays start at zero
Share:
17,495

Related videos on Youtube

Ryan
Author by

Ryan

Updated on September 26, 2022

Comments

  • Ryan
    Ryan about 1 year

    I have to pass two strings to a thread, which will return an array containing both of them, and I must use callable.

    Here's what I have so far:

    public class toArray implements Callable<String[]> {
    
    private String string1 string2;
    
    public toArray (String first, String second)
    {
        string1= first;
        string2 = second;
    }
    
        @Override
        public String[] call() throws Exception {
            String [] allStrings = null;
                allStrings[0] = string1;
                allStrings[1] = string2;
            return allStrings;
        }
    }
    

    Below is the main:

    public class theFuntion{
    
        public static void main(String[] args) {
    
    
            FutureTask<String[]> task = new FutureTask (new MyCallable());
            ExecutorService es = Executors.newSingleThreadExecutor();
            es.submit (task);
            try{
                   String[] result = task.get();
                   System.out.println(result[1] + result[2]);
               }
            catch(Exception e){
                               System.err.println(e);
        }
             es.shutdown();
    }
    

    The problem is in main: it says that result is expecting a String[] but it's getting an object. If I cast result it will say an exception must be declared to be thrown.

    • Radu Murzea
      Radu Murzea about 11 years
      Do you get any errors or strange behaviour ?
    • Makoto
      Makoto about 11 years
      One issue I notice immediately is that you don't state you'll return String[] in your method signature, despite you attempting to do so anyway.
    • Vic
      Vic about 11 years
      Another one is that allStrings is initialized with null and then reused. You, probably wanted String [] allStrings = new String[2]
  • Ryan
    Ryan about 11 years
    "Then, change the return type of call to String[]:" as in return allStrings[]; ???
  • João Silva
    João Silva about 11 years
    @Ryan: No, the return type is what's right after the public accessor. Instead of public String call, use public String[] call. With this, you change the return type from String to String[].
  • Sal
    Sal about 11 years
    Are there an exception? Did you change the initialization of allStrings = null to allStrings = new String[2]? And change the class name toArrayto MyCallable
  • Ryan
    Ryan about 11 years
    i did that edit: FutureTask<String[]> task = new FutureTask(new ToArray("foo", "bar")); , thanks so much but still no change.
  • João Silva
    João Silva about 11 years
    You have many other errors in your code. For example, what's new MyCallable()? Shouldn't it be new toArray("some string", "otherString")? Also, use new FutureTask<String[]>. private String string1 string2; is also not valid, either separate both variables with a comma private String string1, string2; or use two different statements. Finally, arrays in java are 0-indexed, so the first position is [0] instead of [1].