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.
Change the signature of the class to
and the signature of the
callmethod toUPDATE:
Ok, there are more than one error…
See the following working sample