多线程之线程池-forkjoinpool
ForkJoin Pool
Fork: Split task into smaller task
Join: Get Result from immediate subtasks
binary searcy example
- split array and determine the mid element
- check if key equals to mid element, If yes, return
- If element less that mid, recursive search in left half of array
- If element greater than mid, recurisive search in right half of array
- Unless element is found, keep step 3 and step 4
- if array size is 1 and key not euqal to the only element, Return NOT FOUND.
code
import java.util.Arrays; |
import java.util.concurrent.ForkJoinPool;
public class BinarySearch {
public static void searchUsingFJP(int[] input, int key) {
ForkJoinPool forkJoinPool = new ForkJoinPool(2);
ForkJoinSearcher searcher = new ForkJoinSearcher(input, key);
boolean res = (boolean) forkJoinPool.invoke(searcher);
System.out.println(" Element ::" + key +" has been found in array? :: " + res );
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4 ,5, 6, 7, 9 ,10, 20};
BinarySearch.searchUsingFJP(arr, 3);
}
}
import java.util.concurrent.ForkJoinPool; |
work stealing
Worker thread can only execute one task at a time, but ForkJoinPool doesn't create separate thread for every single task, instead thread in the pool has it's own double-ended queue (dequeue) which stores tasks.
By default, work thread get taks from head of its own dequeue, when it's empty, the thread takes task from the tail of dequeu of other busy thread or from global entry queue.