【发布时间】:2015-03-08 21:24:40
【问题描述】:
我正在制作一个多线程应用程序。实现 Runnable 的类有一个返回 ArrayList 的方法。如何在我的 main 中使用该方法?
class SearchThread implements Runnable {
private ArrayList<String> found;
//Constructor
public SearchThread (String[] dataArray) {/**/}
public void run() {
try{
//Do something with found
}
Thread.sleep(time);
System.out.println("Hello from a thread!");
}
catch (Exception e){}
}
public ArrayList<String> getResult() {
return found;
}
}
需要使用getResult方法的主类。
ArrayList<String> result;
Thread[] threads = new Thread[data.length];
for (int i = 0; i < data.length; i++) {
threads[i] = new Thread(new SearchThread(data[i]));
threads[i].start();
}
try {
for (int i = 0; i < data.length; i++) {
threads[i].join();
result = // need to use the getResult()
}
} catch (Exception e) {
}
【问题讨论】:
标签: java multithreading arraylist runnable