...

/

Redesigning the Threading Logic

Redesigning the Threading Logic

Learn how to redesign threading logic.

We'll cover the following...

The bulk of the code in findMatchingProfiles() that remains after we extract collectMatchSets() and process() is threading logic. We could potentially go even one step further and create a generic method that spawns threads for each element in a collection, but let’s work with what we have now. Here’s the current state of the method:

Press + to interact
public void findMatchingProfiles(
Criteria criteria, MatchListener listener) {
ExecutorService executor =
Executors.newFixedThreadPool(DEFAULT_POOL_SIZE);
for (MatchSet set: collectMatchSets(criteria)) {
Runnable runnable = () -> process(listener, set);
executor.execute(runnable);
}
executor.shutdown();
}

Redesigning

Our idea for ...