Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class ThreadUtils {

private static List<ExecutorService> runningExecutors = new ArrayList<>();

private static final int threadPoolCount = Runtime.getRuntime().availableProcessors() * 2;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a magic number heuristic here. It makes sense in context, but I would prefer a safer approach, like the following:

private static final int threadPoolCount = Integer.getInteger("threadutils.pool.count", Runtime.getRuntime().availableProcessors());

This allows configuration to override e.g. -Dthreadutils.pool.count=8

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a magic number heuristic here. It makes sense in context, but I would prefer a safer approach, like the following:

private static final int threadPoolCount = Integer.getInteger("threadutils.pool.count", Runtime.getRuntime().availableProcessors());

This allows configuration to override e.g. -Dthreadutils.pool.count=8

I do like this better. Thank you!


/**
* Executes a list of tasks concurrently using a thread pool.
* <p>
Expand Down Expand Up @@ -62,11 +64,15 @@ public static void executeTasks(List<Callable<Void>> tasks, ExecutorService exec
}

public static void executeTasks(List<Callable<Void>> tasks) {
executeTasks(tasks, Executors.newCachedThreadPool());
ExecutorService executor = Executors.newFixedThreadPool(threadPoolCount);

executeTasks(tasks, executor);
}

public static void executeTasks(Queue<Callable<Void>> callables) {
executeTasks(new ArrayList<>(callables), Executors.newCachedThreadPool());
ExecutorService executor = Executors.newFixedThreadPool(threadPoolCount);

executeTasks(new ArrayList<>(callables), executor);
}

public static void shutdownRunningExecutors() {
Expand All @@ -77,7 +83,7 @@ public static void shutdownRunningExecutors() {
}
runningExecutors = new ArrayList<>();
}catch (Exception e){
//fail silently, shutting down anyways
//fail silently, shutting down anyway
}
}
}
Loading