学习线程 - 只有在另一个方法完成后才运行一个方法 [英] Learning Threads - Running a method only after the other has finished
问题描述
在我的按钮执行中,我调用了 2 个方法.
In my button execution, I am calling 2 methods.
plotButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
startPrinterListOperation();
showAplotPlotterDialog();
}
});
startPrinterListOperation();
需要一些时间来完成它的任务.但我不希望方法 showAplotPlotterDialog();
在第一个完成之前运行.所以我试图使用线程管理来实现这一点.这是我尝试过的.
The startPrinterListOperation();
takes some time to complete its task. But I do not want the method showAplotPlotterDialog();
to run until the first one has completed. So I am trying to use thread management to achieve this. Here is what I have tried.
plotButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Thread t = new Thread() {
public void run() {
startPrinterListOperation();
}
};
t.start();
try {
t.join();
}
catch (InterruptedException e1) {
e1.printStackTrace();
}
showAplotPlotterDialog();
}
});
但是第二种方法在第一种方法完成之前仍然开始.
But the second method stills starts before the first one has finished.
推荐答案
我不会理会线程,这只会让你的程序过于复杂.
I would not bother with threads, this will just make your program overly complicated.
你能编辑 startPrinterListOperation()
方法吗?
我会改为将 showAplotPlotterDialog();
添加到 startPrinter
方法的末尾,以及该方法所做的最后一件事.
I would instead add showAplotPlotterDialog();
to the end of the startPrinter
method, and the last last thing the method does.
这篇关于学习线程 - 只有在另一个方法完成后才运行一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!