我正在尝试创建一个新的线程进程,线程进程结束后,我想从那个class.how中得到一个结果,我可以这样做吗?
例如这两个类。假设abThread类返回字符串数组。我应该如何获取这些字符串值。
Class A{
public static void main(String[] args){
abThread bb=new abThread();
bb.start();
// when bb.run() returns data catch it
}
}
Class abThread extends Thread{
public void run(){
**// do smth here**
// then return result to the main Class
}
}
发布于 2016-03-16 13:17:38
您正在寻找的是一个可调用的,如下所示:
public class MyCallable implements Callable<String[]>
{
@Override
public String [] call() throws Exception
{
//Do your work
return new String[42]; //Return your data
}
}
public static void main(String [] args) throws InterruptedException, ExecutionException
{
ExecutorService pool = Executors.newFixedThreadPool(1);
Future<String[]> future = pool.submit(new MyCallable());
String[] myResultArray = future.get();
}
https://stackoverflow.com/questions/36036489
复制相似问题