![](/img/trans.png)
[英]in java: how can i terminate execution of all threads without waiting for them to finish their jobs using ExecutorService?
[英]How to run multiple java runtime jobs and wait for all of them to finish?
我有以下代碼可以在Java中使用進度條運行作業:
private void createNewJobRun(final int i) {
Job job = new Job("Experiment " //$NON-NLS-1$
+ getName()) {
@Override
protected IStatus run(IProgressMonitor monitor) {
monitor.beginTask("Experiment is running ...", 100); //$NON-NLS-1$
//call here my own functions that to the actual work
monitor.done();
return Status.OK_STATUS;
}
};
job.schedule();
}
問題是我想多次調用此函數,但只有第一個作業運行,而其他作業幾乎同時啟動,但我從未看到它們的結果...盡管顯示了它們的進度監視器。 我該如何等待所有人都以某種方式完成,或者問題出在別的東西上?
首先,您必須使自己的類成為Callable的子類。
在此類中,您將重寫調用方法,並執行您在createNewJobRun中所做的事情。
現在,您必須創建一個FutureTask(在Thread中執行以創建Job並獲得結果)。
示例:查看此鏈接(標題為FutureTask <V>
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.