Created
December 11, 2018 13:29
-
-
Save nachogarrone/69f2277178664fae1982da9ae3f6dd52 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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