Forkjointask and forkjoinpool of j.u.c extension component in Java

Entry station 2021-01-20 21:43:06
forkjointask forkjoinpool j.u.c extension component


Fork/Join Two core classes in the framework ForkJoinTask And ForkJoinPool, Statement ForkJoinTask after , Add it to ForkJoinPool in , And return a Future object .
  • ForkJoinPool:ForkJoinTask Need to pass through ForkJoinPool To execute , The subtasks of task segmentation will be added to the double ended queue of current work maintenance , The head of the queue . When there are no tasks in the queue of a worker thread , It randomly fetches a task from the end of the queue of other worker threads .
  • ForkJoinTask: We need to use ForkJoin frame , First, create a ForkJoin Mission . It provides for performing... In tasks Fork() and Join() The mechanism of operation , In general, you don't need to inherit directly ForkJoinTask class , You just need to inherit its subclasses ,Fork/Join The framework provides the following two subclasses .
  • RecursiveAction: For tasks with no return value .
  • RecursizeTask: For tasks with return values .

image-20210120194025938

Exception

ForkJoinTask Exceptions may be thrown during execution , But we can't catch exceptions directly in the main thread , therefore ForkJoinTask Provides isCompletedAbnormally() Method to check whether the task has thrown an exception or has been cancelled , And through ForkJoinTask Of getException Method to catch exceptions .
public abstract class ForkJoinTask<V> implements Future<V>, Serializable {
/** ForkJoinTask Running state */
volatile int status; // Directly be ForkJoin Pool and worker access
static final int DONE_MASK = 0xf0000000; // mask out non-completion bits
static final int NORMAL = 0xf0000000; // must be negative
static final int CANCELLED = 0xc0000000; // must be < NORMAL
static final int EXCEPTIONAL = 0x80000000; // must be < CANCELLED
static final int SIGNAL = 0x00010000; // must be >= 1 << 16
static final int SMASK = 0x0000ffff; // short bits for tags
/**
* @Ruturn Whether the task throws an exception or is cancelled
*/
public final boolean isCompletedAbnormally() {
return status < NORMAL;
}
/**
* If the calculation throws an exception , Then return an exception
* If the task is cancelled, return to CancellationException. If the task is not completed or an exception is not thrown, it returns null
*/
public final Throwable getException() {
int s = status & DONE_MASK;
return ((s >= NORMAL) ? null :
(s == CANCELLED) ? new CancellationException() :
getThrowableException());
}
}

ForkJoinPool Source code

public class ForkJoinPool extends AbstractExecutorService {
/**
* ForkJoinPool, It is the same as ThreadPoolExecutor equally , It has also been realized. Executor and ExecutorService Interface . It has been used.
* An infinite queue to hold the tasks that need to be performed , The number of threads is passed in through the constructor , If you don't pass in the constructor
* Number of threads expected , So the current computer available CPU The number is set to the number of threads as the default .
*/
public ForkJoinPool() {
this(Math.min(MAX_CAP,Runtime.getRuntime().availableProcessors()),
defaultForkJoinWorkerThreadFactory, null, false);
}
public ForkJoinPool(int parallelism) {
this(parallelism, defaultForkJoinWorkerThreadFactory, null, false);
}
// There are multiple constructors , It is omitted here
volatile WorkQueue[] workQueues; // main registry
static final class WorkQueue {
final ForkJoinWorkerThread owner; // The worker thread
ForkJoinTask<?>[] array; // Mission
// What's coming in is ForkJoinPool With a specified worker thread
WorkQueue(ForkJoinPool pool, ForkJoinWorkerThread owner) {
this.pool = pool;
this.owner = owner;
// Place indices in the center of array (that is not yet allocated)
base = top = INITIAL_QUEUE_CAPACITY >>> 1;
}
}
}

FrokJoinPool work stealing Algorithm

38f0c9a9a9b072b589aba560a849c18bad5.jpg

ForkJoinPool Maintained a group of WorkQueue, That's the work queue , Another worker thread is maintained in the work queue ForkJoinWorkerThread With a group of tasks ForkJoinTask

WorkQueue It's a double ended queue Deque(Double Ended Queue),Deque It's a data structure with the properties of queue and stack , Elements in a two terminal queue can pop up from both ends , It limits the insertion and deletion operations on both sides of the table .

Each worker thread creates a new task at run time ( Usually because it calls fork()) when , Will be placed at the end of the work queue , And when the worker thread is processing its own work queue , It uses LIFO, That is to say, every time you take a task from the end of the queue to execute .

Each worker thread is processing its own work queue at the same time , Will try to steal a task ( Or from just submitted to pool The task of , Or work queues from other worker threads ), The stolen task is at the head of the work queue of other threads , That is to say, when a worker thread steals tasks from other workers , It uses FIFO The way .

In case of Join() when , if necessary Join Tasks for have not been completed , Priority will be given to other tasks , And wait for it to finish .

When you don't have a mission of your own , And there's nothing to steal , Then go to sleep .

public class ForkJoinPool extends AbstractExecutorService {
public <T> ForkJoinTask<T> submit(ForkJoinTask<T> task) {}
public <T> ForkJoinTask<T> submit(Callable<T> task) {}
public <T> ForkJoinTask<T> submit(Runnable task, T result) {}
public ForkJoinTask<?> submit(Runnable task) {}
}
ForkJoinPool It has its own work queue , These work queues are used to receive messages from external threads ( Not ForkJoinThread Threads ) The task submitted , And these work queues are called submitting queue .

ForkJoinTask

The operation of the task , It is important to fork() and join().
public abstract class ForkJoinTask<V> implements Future<V>, Serializable {
/**
* Execute this task asynchronously in the pool where the current task is running ( If applicable )
* Or use ForkJoinPool.commonPool()( If not ForkJoinWorkerThread example ) To execute asynchronously
*/
public final ForkJoinTask<V> fork() {
Thread t;
if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread)
((ForkJoinWorkerThread)t).workQueue.push(this);
else
ForkJoinPool.common.externalPush(this);
return this;
}
public final V join() {
int s;
if ((s = doJoin() & DONE_MASK) != NORMAL)
reportException(s);
return getRawResult();
}
private int doJoin() {
int s; Thread t; ForkJoinWorkerThread wt; ForkJoinPool.WorkQueue w;
return (s = status) < 0 ? s :
((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
(w = (wt = (ForkJoinWorkerThread)t).workQueue).
tryUnpush(this) && (s = doExec()) < 0 ? s :
wt.pool.awaitJoin(w, this, 0L) :
externalAwaitDone();
}
}
fork() There's only one thing to do , Push the current task into the work queue of the current thread .

join() My work is more complicated , It's also join() The reason why the thread can be prevented from being blocked .

  • Check call join() Is the thread of ForkJoinThread Threads . If not ( for example main Threads ), Block the current thread , Waiting for the task to complete . If it is , It doesn't block .
  • Check the completion status of the task , If completed , Then directly return the result .
  • If the task is not completed , But dealing with your own work queue , Then complete it .
  • If the task has been stolen by another thread , Then the tasks of this work queue are executed in a first in first out manner , Help thief thread finish as soon as possible join
  • If the thief who stole the task has finished all his tasks , Waiting for the need to join The task of , Find the thief ( Recursive execution ), Help it do its job .

bc8ab44bbad490edddfe5b6e8febaab2742.jpg

ForkJoinPool.submit Method

public static void main(String[] args) throws ExecutionException, InterruptedException {
// Generate a pool
ForkJoinPool forkJoinPool=new ForkJoinPool();
ForkJoinTask task=new ForkJoinExample(1, 100000);
ForkJoinTask<Integer> submit = forkJoinPool.submit(task);
Integer sum = submit.get();
System.out.println(" The end result is :"+sum);
}
Each worker thread has its own work queue , ForkJoinPool It has its own work queue , These work queues are used to receive external threads ( Not ForkJoinPool) The task submitted , And these work queues are called submitting queue.

submit() and fork() There is no essential difference , It's just that the submitted object becomes submitting queue( And some initialization , Synchronous operation ).submitting queue And other work queue equally , Is the object that worker thread steals , So when one of the tasks is successfully stolen by a worker thread , This means that the submitted task really begins to enter the implementation phase .

Pay attention to WeChat public number :【 Entry station 】, Unlock more knowledge .

版权声明
本文为[Entry station]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210120214234980i.html

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云