我在Java中使用FixedThreadPool來處理圖像。問題是我有什麼,首先我解析一個xml,用這個xml的信息我開始在池中的線程(用於下載的東西)。使用fixedthreadpool java如何檢查是否完成
現在的問題是,我在每個<標籤> xml中填充信息的ThreadPool。所以開始我不知道有多少次我將不得不將線程添加到池中。例如:
XML:
<object>
<download></download>
<download></download>
<download></download>
</object>
<object>
<download></download>
<download></download>
</object>
我創建的下載信息的ArrayList,而且比它傳遞給線程池。
線程池如下:
public void addAnhang(ArrayList<Anhang> a, String intern_id){
this.generateFolder(intern_id);
for(Anhang anh:a){
anh.setZielordner(props.getProperty("zielordner_anhang")+intern_id+File.separator);
this.tasks.add(Executors.callable(new AnhangWorker(anh)));
//this.pool.execute(new AnhangWorker(anh));
}
try {
this.pool.invokeAll(tasks);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("end of this cycle");
}
所以現在我的問題。我如何知道我的所有線程何時完成? xml解析器比下載更快,所以我無法在xml完成後終止所有線程。但我不知道何時線程池完成下載所有文件。
確定,但其參數我需要在awaitTermination(這可能是因爲線程池需要5個多分鐘的XML解析器) –
我想是這樣的: 而(this.poool.awaitTermination(10,TIMEUNIT .SECONDS)) System.out.println(「pool still running」); 但它也不關閉線程完成後.. –
這應該工作,但沒有循環的一點,除非你需要在循環中做一些事情。 – Gray