Using callable to pass two parameters and returning an array of parameters
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
Related videos on Youtube
Ryan
Updated on September 26, 2022Comments
-
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 about 11 yearsDo you get any errors or strange behaviour ?
-
Makoto about 11 yearsOne 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 about 11 yearsAnother one is that
allStrings
is initialized withnull
and then reused. You, probably wantedString [] allStrings = new String[2]
-
-
Ryan about 11 years"Then, change the return type of call to String[]:" as in return allStrings[]; ???
-
João Silva about 11 years@Ryan: No, the return type is what's right after the
public
accessor. Instead ofpublic String call
, usepublic String[] call
. With this, you change the return type from String to String[]. -
Sal about 11 yearsAre there an exception? Did you change the initialization of
allStrings = null
toallStrings = new String[2]
? And change the class nametoArray
toMyCallable
-
Ryan about 11 yearsi did that edit: FutureTask<String[]> task = new FutureTask(new ToArray("foo", "bar")); , thanks so much but still no change.
-
João Silva about 11 yearsYou have many other errors in your code. For example, what's
new MyCallable()
? Shouldn't it benew toArray("some string", "otherString")
? Also, usenew FutureTask<String[]>
.private String string1 string2;
is also not valid, either separate both variables with a commaprivate String string1, string2;
or use two different statements. Finally, arrays in java are0-indexed
, so the first position is [0] instead of [1].