当前位置:网站首页>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 ~
边栏推荐
- Wechat development tool (applet)
- Source code interpretation of detectron2 1--engine
- Opencv learning notes -day8 (keyboard typing (waitkey()); Wait for typing) action: triggers some action when the appropriate character is typed using the keyboard)
- [untitled]
- C#訪問SQL Server數據庫兩種方式的比較(SqlDataReader vs SqlDataAdapter)
- Esp32 things (x): other functions
- CUDA implements matrix replication
- Mmcv expanding CUDA operator beginner level chapter
- Anchorgenerator for mmdet line by line interpretation
- Invalid update: invalid number of sections. The number of sections contained in the table view after
猜你喜欢
Flink 数据偶尔数据积压导致checkpoint失败
Detectron2 source code reading 3-- encapsulating dataset with mapper
将线程绑定在某个具体的CPU逻辑内核上运行
Evaluation standard for audio signal quality of intelligent speakers
Build a docker image of Henkel database from 0
Opencv learning notes-day14 drawing of image geometry (rect class rotatedrect class, rectangle drawing rectangle circle drawing circular function line drawing line function ellipse drawing elliptic fu
Rew acoustic test (VI): signal and measurement
Mmdet line by line deltaxywhbboxcoder
使用华为性能管理服务,按需配置采样率
Wechat development tool (applet)
随机推荐
JVM调优相关命令以及解释
mysql基础入门 动力节点[老杜]课堂作业
[untitled]
Use Huawei performance management service to configure the sampling rate on demand
Opencv learning notes -day10 logical operation of image pixels (usage of rectangle function and rect function and bit related operation in openCV)
Detailed explanation of pipline of mmdetection
C accesses mongodb and performs CRUD operations
技术管理进阶——管理者如何进行梯队设计及建设
VIM from dislike to dependence (21) -- cross file search
使用华为性能管理服务,按需配置采样率
File upload component on success event, add custom parameters
Wikimedia Foundation announces the first customers of its new commercial product "Wikimedia enterprise"
Summary of common pytoch APIs
Opencv learning notes -day 11 (split() channel separation function and merge() channel merge function)
Esp32 things (VIII): music playing function of function development
Esp32 (7): I2S and I2C drivers for function development
Axure make menu bar effect
vite项目require语法兼容问题解决require is not defined
Sort (simple description)
将线程绑定在某个具体的CPU逻辑内核上运行