Sie müssen ALLE ausnahmslos ordnungsgemäß beendet werden.
Wenn einer ausfällt, muss ich sie alle sofort stoppen und darf nicht Minuten warten, bis sie einen Job abgeschlossen haben, der nutzlos ist. Wie kann ich das machen? Dies ist ein Teil meines aktuellen Codes:
Code: Select all
ExecutorService executorService = Executors.newFixedThreadPool(iNumThread);
List callableTasks = new ArrayList();
for (int i = 0; i < iNumThread; i++) {
callableTasks.add(new ExecuteJob(...));
}
List resultList = null;
try {
resultList = executorService.invokeAll(callableTasks);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
executorService.shutdown();
// This cycle doesn't work for me because it waits for all threads to finish even if one or more have given an exception.
while (!executorService.isTerminated()) {
//DO SOMETHING LIKE UPDATE UI, ETC
}
//check result
for (int i = 0; i < resultList.size(); i++) {
Future future = resultList.get(i);
try {
String result = future.get();
if (!result.equals("OK")){
//ohi ohi
}
} catch (InterruptedException | ExecutionException e) {
//ohi ohi
e.printStackTrace();
}
}
Ich hoffe, ich habe mich klar ausgedrückt. Vielen Dank.
Max
Mobile version