java 使用 callable 传递两个参数并返回参数数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12166879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using callable to pass two parameters and returning an array of parameters
提问by Ryan
I have to pass two strings to a thread, which will return an array containing both of them, and I must use callable.
我必须将两个字符串传递给一个线程,该线程将返回一个包含这两个字符串的数组,并且我必须使用 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.
问题在于main
:它说结果需要一个 String[] 但它正在获取一个对象。如果我转换结果,它会说必须声明要抛出异常。
回答by Sal
Change the signature of the class to
将类的签名更改为
public class toArray implements Callable<String[]>
and the signature of the call
method to
和call
方法的签名
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;
}
}
}
回答by Jo?o Silva
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[]
:
在 中Callable<V>
,参数化类型V
代表 的结果类型call()
。由于您想返回 a String[]
,而不是Callable<String>
,请使用Callable<String[]>
. 然后,将返回类型更改call
为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 shouldbe nouns, and should start with an uppercase letter.
作为旁注,Java 中的类名应该是名词,并且应该以大写字母开头。
回答by Reimeus
You have not referenced your ToArray
class in TheFunction
, probably due to compilation errors (hint: fixes have already been mentioned).
您没有在 中引用您的ToArray
类TheFunction
,可能是由于编译错误(提示:已经提到了修复)。
Your new FutureTask will look like:
您的新 FutureTask 将如下所示:
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
:
在不告诉您答案的情况下,您将在以下call()
方法中遇到 2 个异常ToArray
:
- Firstly your String array is never initialised, but shouldbe.
- All arrays start at zero
- 首先,您的 String 数组从未初始化,但应该初始化。
- 所有数组都从零开始