Skip to content

Instantly share code, notes, and snippets.

@nachogarrone
Created December 11, 2018 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nachogarrone/69f2277178664fae1982da9ae3f6dd52 to your computer and use it in GitHub Desktop.
Save nachogarrone/69f2277178664fae1982da9ae3f6dd52 to your computer and use it in GitHub Desktop.
@Component
public class MyForkJoinPool {
private static final Logger LOGGER = LoggerFactory.getLogger(MyForkJoinPool.class);
private ForkJoinPool forkJoinPool;
private ForkJoinPool hardForkJoinPool;
@PostConstruct
public void init() {
this.forkJoinPool = new ForkJoinPool(2);
this.hardForkJoinPool = new ForkJoinPool(4);
}
public void submit(Runnable task) {
if (this.forkJoinPool != null) {
this.forkJoinPool.execute(task);
} else {
LOGGER.error("Fork Join Pool is null");
}
}
public void submitHard(List<Runnable> tasks) {
if (this.hardForkJoinPool != null) {
List<ForkJoinTask> fjTasks = new ArrayList<>();
tasks.forEach(t -> fjTasks.add(this.hardForkJoinPool.submit(t)));
LOGGER.info("Tasks submitted: {}", tasks.size());
fjTasks.forEach(ForkJoinTask::join);
LOGGER.info("Tasks finished: {}", fjTasks.size());
} else {
LOGGER.error("Fork Join Pool is null");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment