Part 11: java.util.concurrent - CachedThreadPool Example(二)

2014-11-24 09:04:21 · 作者: · 浏览: 1
iles = dir.list();
/**
* Optimization - Reuse the the threads.
*/
executorPool = Executors.newCachedThreadPool();

for (String file : files) {
Callable reader = new CachedReader(dir.getAbsolutePath() + File.separator + file);
fileList.add(reader);
}
List> results = executorPool.invokeAll(fileList);
/**
* Check how many success and how many failure
*/
int success = 0, failure = 0;
for (Future result : results) {
if (result.get() == null) {
failure++;
} else {
success++;
}

}
System.out.println("Total number of files [" + fileList.size() + "]");
System.out.println("Success Count [" + success + "]");
System.out.println("Failure Count [" + failure + "]");
} else {www.2cto.com
throw new IllegalArgumentException("There is no such directory name -" + directory);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (executorPool != null) {
executorPool.shutdown();
}
}
}
}
作者:沉默是金