当前位置:网站首页>Talk about the kotlin cooperation process and the difference between job and supervisorjob
Talk about the kotlin cooperation process and the difference between job and supervisorjob
2022-06-30 09:01:00 【Byte station】
1. Preface
With the popularity of collaborative processes , Collaborative process knowledge is increasingly favored by interviewers . First , The interview questions of Xie Cheng are usually very concise , One or two simple words can describe the problem clearly , Second, for the interviewer , The exquisite data structure and algorithm in the collaborative process framework can well examine the applicant's mastery of basic knowledge . Compared to the of rotten Street Android stereotyped writing , It can also examine the candidate's active learning ability . The so-called expert knows whether there is . Collaborative process knowledge is the expert and touchstone in the interview scene . Gossip and less narration , This article takes you to talk about Job and SupervisorJob The difference between .
2. Job and SupervisorJob The difference between
If you have seen the official documents or videos of the cooperation process . You should know Job and SupervisorJob One difference is ,Job If an exception occurs in the sub process of, it will be cancelled at the same time Job Other subprocesses , and SupervisorJob Can't .
Job Launched the 3 The subprocess job1、job2、job3.job1 delay 100 An exception occurred after milliseconds , The collaboration was cancelled ,job2 and job3 It was also cancelled . 
SupervisorJob Launched the 3 The subprocess job1、job2、job3.job1 delay 100 An exception occurred after milliseconds , The collaboration was cancelled ,job2 and job3 Not affected . 
3. principle
Friends who have had interview experience should know , Interviewers like to ask principles . Why? Job and SupervisorJob There is such a difference ?

Find the answer in the source code .Job() The return is JobImpl object ,SupervisorJob() Back to SupervisorJobImpl object . and SupervisorJobImpl yes JobImpl Subclasses of , And rewritten childCancelled Method , The return value is false.JobImpl Inherited from JobSupport, its childCancelled The source code of the method is as follows :

Job How to establish a parent-child relationship One article , Have explained childCancelled Method . Start the process Job And the process itself Job Form a father son relationship . When the collaboration is cancelled , Will call the parent Job Of childCancelled Method .SupervisorJobImpl Go straight back to false, Represent parent Job Not because Job Be cancelled and follow the cancellation . and JobSupport Would call cancelImpl Method , This method cancels the parent Job Father Job All son Job.
4. The difference between exception handling
congratulations , You are lucky to pass the previous questions , Just when you are secretly happy . The interviewer may ask , Excuse me, except for Job The cancellation of is different , Is there any other difference ? Of course there is , For example, the exception handling mechanism is different ,Job If an exception occurs in the process started by the method , The exception will follow Job The tree has been passing up , and SupervisorJob An exception occurred in the co process started by the method ,SupervisorJob The exception will be handled by the coroutine . That's a little abstract , Look at an example .
Demonstrate collaboration nesting , There is Job When the type starts the collaboration , If the subprocess is abnormal , Exceptions will be handled by the root coroutine . 
Demonstrate collaboration nesting , There is SupervisorJob When the type starts the collaboration , If the subprocess is abnormal , The exception will be handled by the subprocess . 
The answer is , I thought to myself , There's nothing wrong with it now , But the interviewer then asked , What is the principle ? Of course, the answer should be found in the source code . 
Code 1 It's about , Is the core logic for handling exceptions , It first judges cancelParent The return value of , If you return false, Just call handleJobException.
cancelParent The meaning of the method is , When an exception occurs at the subprocess , Then it will try to cancel its parent process , If you return true Indicates that the parent process is also cancelled , Conversely, it means that the parent process cannot be cancelled , and cancelParent Eventually, it is also possible to call the code 2 Situated parent.childCancelled Method .
The exception handling logic can be explained by workplace examples . Suppose the unspoken rule of the workplace is , Any employee made a mistake , The first thing is to report to the superior , If your superiors are willing to deal with your mistakes , Then the employees don't have to care , If the supervisor calls back the problem to the employee , Then the mistakes have to be handled by the employees themselves
So back to the question itself ,Job It's equivalent to a good boss , The mistake made by zixie Cheng , It is willing to deal with ,SupervisorJob It's like a tough boss , Zixie Cheng's own mistake , Solve it yourself .
The processing mechanism of the coroutine exception is very complex , It is beyond the scope of this article , I will be on my knowledge planet , Explain in another article .
5. Is that enough ?
Writing at this point ,Job and SupervisorJob Make it clear. . But there are many other problems , such as :
- CancellationException And other Exception The difference between
- coroutineScope And SupervisorScope The difference between
Remember to pay attention to " Byte station " Official account to get more dry goods knowledge ~
边栏推荐
- Understanding society at the age of 14 - reading notes on "happiness at work"
- Opencv learning notes -day13 pixel value statistics calculation of maximum and minimum values, average values and standard deviations (use of minmaxloc() and meanstddev() functions)
- [untitled]
- 【付费推广】常见问题合集,推荐榜单FAQ
- Opencv learning notes-day9 opencv's own color table operation (colormap coloraptypes enumeration data types and applycolormap() pseudo color function)
- CUDA realizes matrix multiplication
- Advanced technology management -- how managers design and build echelons
- mysql基础入门 day4 动力节点[老杜]课堂笔记
- QT downloading files through URL
- [untitled]
猜你喜欢

Summary of common pytoch APIs

Opencv learning notes -day13 pixel value statistics calculation of maximum and minimum values, average values and standard deviations (use of minmaxloc() and meanstddev() functions)

codeforces每日5题(均1700)-第三天

Explanation on the use of password profiteering cracking tool Hydra

Deploy the cow like customer network project on the ECS

Evaluation standard for audio signal quality of intelligent speakers
![[untitled]](/img/45/368c41a74bf0738369c58c5963fca0.jpg)
[untitled]

Duplicate entry '2' for key 'primary appears in JPA‘

Pytorch BERT

C#访问MongoDB并执行CRUD操作
随机推荐
14岁懂社会-《关于“工作的幸福”这件事儿》读书笔记
C accesses mongodb and performs CRUD operations
证券开户的优惠怎样才能得到?在线开户安全?
文件上传 upload 组件 on-success 事件,添加自定义参数
mysql基础入门 动力节点[老杜]课堂作业
Pytorch BERT
icon资源
快应用中实现自定义抽屉组件
Esp32 things (x): other functions
C # listbox how to get the selected content (search many invalid articles)
Flink sql -- No factory implements ‘org. apache. flink. table. delegation. ExecutorFactory‘.
Mmcv expanding CUDA operator beginner level chapter
[untitled]
Opencv learning notes -day4 image pixel reading and writing operations (array traversal and pointer traversal implementation, uchar vec3b data type and mat class functions mat:: at(), mat:: ptr())
Mmdet line by line code interpretation of positive and negative sample sampler
Maxiouassigner of mmdet line by line interpretation
Flink Exception -- No ExecutorFactory found to execute the application
Wechat development tool (applet)
技术管理进阶——管理者如何进行梯队设计及建设
Unsupportedclassversionerror is reported when starting jar package. How to repair it