Java ForkJoin Pool
# Java ForkJoin Pool
1. As it sounds, assist distribute work
2. From java 7
3. Based on Doug Lea's work
4. As it sounds, break to simpler tasks
1. Ideally, no worker thread idle
1. Threads steal work
5. Classes you use
1. ForkJoinPool
1. Implements ExecutorService
2. `ForkJoinPool = new ForkJoinPool(int parallelism)`
1. With no arg by default, numProcessors
3. Pool adjusts its size! (though you specified number)
1. Send ForkJoinPool Tasks with
1. Execute() - asynchronous
2. invoke() - sync awaits
3. submit() - Async get Future
2. ForkJoinTask
1. isDone() - true when complete
2. isCompletedNormally() - finished without exception
3. isCompletedAbnormally() - true cancelled / exception
6. How to use
1. Create `RecursiveTask`
2. Implement `compute()` in RecursiveTask to do your work
1. You can create additional tasks inside it, that's the whole point
2. In additional tasks in `RecursiveTask` call `task.fork()` put them in an internal `List`
3. Finally, loop the internal `List` of internal tasks and wait for them to `join()`
3. `ForkJoinPool pool = new ForkJoinPool`
4. `pool.execute(myTask1)`
5. `pool.execute(myTask2)`
6. …
7. Main points for ForkJoinPool
1. Threads in pool steal work
2. Tasks create subtasks
3. As a result
1. Threads maximize their runtime.
Comments
Post a Comment