当前位置:网站首页>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 ~
边栏推荐
- PHP API to obtain QR code and combine to generate pictures
- Qt连接神通数据库
- How can I get the discount for opening a securities account? Is online account opening safe?
- Detectron2 source code reading 4-- registrar construction model
- Interpretation of source code demand:a rotation equivariant detector for aerial object detection
- Rew acoustic test (IV): test principle of rew
- Wechat development tool (applet)
- Opencv learning notes -day 11 (split() channel separation function and merge() channel merge function)
- Detectron2 source code reading 2--- using the configurable decorator to build the dataloader
- Duplicate entry '2' for key 'primary appears in JPA‘
猜你喜欢

Opencv learning notes -day10 logical operation of image pixels (usage of rectangle function and rect function and bit related operation in openCV)

VIM from dislike to dependence (21) -- cross file search

Introduction to the runner of mmcv

Opencv learning notes-day5 (arithmetic operation of image pixels, add() addition function, subtract() subtraction function, divide() division function, multiply() multiplication function

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())

Redis design and Implementation (IV) | master-slave replication

Explanation on the use of password profiteering cracking tool Hydra

Tidb 6.0: making Tso more efficient tidb Book rush

Opencv learning notes-day6-7 (scroll bar operation demonstration is used to adjust image brightness and contrast, and createtrackbar() creates a scroll bar function)

Rew acoustic test (I): microphone calibration
随机推荐
Raspberry pie 4B no screen installation system and networking using VNC wireless projection function
Sort (simple description)
Opencv learning notes -day1 (image reading display imread, imshow, namedwindow)
Vite project require syntax compatibility problem solving require is not defined
Opencv learning notes -day2 (implemented by the color space conversion function cvtcolar(), and imwrite image saving function imwrite())
Esp32 things (VIII): music playing function of function development
Redis design and Implementation (I) | data structure & object
Be careful of this hole in transmittable thread local
Qt通过Url下载文件
CUDA realizes matrix multiplication
Opencv learning notes -day3 (mat object and creation related operations mat:: clone(), mat:: copyto(), mat:: zeros(), mat:: ones(), scalar()...)
[paid promotion] collection of frequently asked questions, FAQ of recommended list
技术管理进阶——管理者如何进行梯队设计及建设
C accesses mongodb and performs CRUD operations
JVM调优相关命令以及解释
Qt连接神通数据库
c#获取当前的时间戳
Flink Sql -- toAppendStream doesn‘t support consuming update and delete changes which
Set, map and modularity
Wechat development tool (applet)