我正在玩 SwingWorker 并从使用它中学习。所以,我有这个代码:
ArrayList<Train> dataList = new ArrayList<>();
ArrayList<Train> proccessedData = new ArrayList<>();
String query = "my query, not relevant here";
dataList = myApi.getTrainData(connectServer(), query, false);
TestNewThread tpp = new TestNewThread(dataList, threshold, pairs, false);
tpp.execute();
我的 TestNewThread 类是:
public class TestNewThread extends SwingWorker<List, String>{
private ArrayList<Train> dataList;
private int threshold
private int pair
private boolean processFurther)
MyAPI myApi = MyAPI.getInstance();
public TestNewThread(ArrayList<Train> dataList, int threshold, int pair, boolean processFurther) {
this.dataList = dataList;
this.threshold = threshold
this.pair = pair
this.iprocessFurther) = processFurther)
}
@Override
protected List doInBackground() throws Exception {
ArrayList<Train> list;
publish("Starting process");
list = processingTrains(threshold, pair, dataList);
publish("Process finished.");
return list;
}
@Override
protected void process(List<String> chunks) {
String mostRecentValue = chunks.get(chunks.size() - 1);
myApi.printPanel("\n"+mostRecentValue);
}
@Override
protected void done() {
// myApi .printPanel("\nALL DONE... !);
}
现在我需要但我不知道该怎么做:在此处的第一段代码中,tpp.execute();
我希望它返回分配的变量。对于这样的事情,请注意评论部分:
ArrayList resultsList = new ArrayList();
TestNewThread tpp = new TestNewThread(dataList, threshold, pairs, false);
resultsList = tpp.execute(); // I know I can't do this because .execute() is void but I want something like whatever the new thread returns to return to resultsList array.
如果代码中有任何拼写错误或其他内容,我很抱歉,我现在只是从头顶上解释我的问题。我想你可以从这里得到这个想法。