How can the number of threads executing in an Executor
be limited? For example, in the code bellow:
executor = Executors.newFixedThreadPool(this.noOfThreads);
for (int i = 0; i < sections.size(); i++) {
DomNode section = sections.get(i);
// Retrieve the url of the subsection
String subsectionUrl = section.getNodeValue();
if(i != 0 && (i % noOfThreadsPerSection == 0)) {
// Add section threads to the executor
executor.execute(new BrowseSection(filter, webClient, subsectionUrl));
} else {
executor.shutdown();
try {
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
// We should normally never get here
}
}
}
when executor.execute()
is called, what happens if the thread pool is filled with running threads?
The code above does not check whether threads are still running in the executor, but rather the number of threads that have been launched.