当前位置:网站首页>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 ~
边栏推荐
- Metasploit practice - SSH brute force cracking process
- 证券开户的优惠怎样才能得到?在线开户安全?
- Viteproject require Syntax Compatibility Problem Solving require is not defined
- Detectron2 source code reading 2--- using the configurable decorator to build the dataloader
- Comparaison de deux façons d'accéder à la base de données SQL Server (sqldatareader vs sqldataadapter)
- Design specification for smart speakers v1.0
- [untitled]
- Resnet50+fpn for mmdet line by line code interpretation
- layer. Open processing method when the passed value is an array or the value is too long
- A troubleshooting of CPU bottom falling
猜你喜欢

Detailed explanation of pipline of mmdetection

Use Huawei performance management service to configure the sampling rate on demand

Flink Sql -- toAppendStream doesn‘t support consuming update and delete changes which

C accesses mongodb and performs CRUD operations

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

技术管理进阶——管理者如何进行梯队设计及建设

Build a docker image of Henkel database from 0
![[kotlin collaboration process] complete the advanced kotlin collaboration process](/img/43/9c4b337caf406537e317dea2ed5f17.png)
[kotlin collaboration process] complete the advanced kotlin collaboration process

Does the oscilloscope probe affect the measurement of capacitive load?

Duplicate entry '2' for key 'primary appears in JPA‘
随机推荐
Wikimedia Foundation announces the first customers of its new commercial product "Wikimedia enterprise"
Tidb 6.0: making Tso more efficient tidb Book rush
Implementing custom drawer component in quick application
asdsadadsad
CUDA implements matrix replication
[untitled]
Introduction to MySQL basics day4 power node [Lao Du] class notes
CUDA realizes L2 European distance
127.0.0.1、0.0.0.0和localhost
Redis design and Implementation (V) | sentinel sentry
How can we get a satisfactory salary? These routines still need to be mastered
[cmake] make command cannot be executed normally
Rew acoustic test (I): microphone calibration
Six implementation methods of singleton mode
Resnet50+fpn for mmdet line by line code interpretation
C accesses mongodb and performs CRUD operations
Design specification for smart speakers v1.0
【付费推广】常见问题合集,推荐榜单FAQ
About Lombok's @data annotation
Set, map and modularity