1. Introduction

With the popularity of coroutines, the knowledge of coroutines is favored by more and more interviewers. First of all, the interview questions of coroutine are generally very concise, one or two sentences can clearly describe the question. Secondly, for the interviewer, the sophisticated data structure and algorithm in coroutine framework can well test the degree of the applicant’s mastery of basic knowledge. Compared with the Android eight-part essay on rotten street, it can also examine the active learning ability of applicants. The so-called expert will know whether there is a hand. Coroutine knowledge is an expert and touchstone in the interview scenario. Without further ado, this is the difference between the Job and the Job it is handling.

2. The difference between Job and SupervisorJob

If you look at the official documentation or videos of coroutines. You should know that the difference between the Job and the container is that the child coroutine of the Job is going to be canceled when it’s abnormal, and the other child coroutines of the Job are going to be canceled, while the container is not.

Job starts three subcoroutines job1, joB2, and Job3. Job2 and job3 were cancelled after delay 100 ms.

It is designed to enable three subroutines joB1, JOB2 and Job3. After job1 delay 100 ms, an exception occurred. The coroutine was cancelled. Job2 and Job3 were not affected.

Principle 3.

Friends who have had interview experience should know that interviewers like to ask principles. Why is there such a difference between the Job and the SupervisorJob?

Source code for the answer. The Job() container is handling the JobImpl object it is handling. The Job() container is handling the JobImpl object it is handling. The SupervisorJobImpl is a subclass of JobImpl, and it overrides the childCancelled method to return false. JobImpl inherits from JobSupport. Its childCancelled method is as follows:

ChildCancelled method was explained in a paper by Job on how to establish a relationship between father and son. The Job that starts the coroutine forms a parent-child relationship with the Job of the coroutine itself. When the coroutine is cancelled, the childCancelled method of the parent Job is called. SupervisorJobImpl returns false, indicating that the parent Job will not be canceled when the child Job is canceled. JobSupport calls the cancelImpl method, which cancels the parent Job and all of its child jobs.

4. Differences in exception handling

Congratulations, you are lucky to pass the previous questions, just when you secretly congratulate yourself. Other than the cancellation of sub-jobs, the interviewer may ask, are there any other differences? For example, the exception handling mechanism is different, so the coroutine that’s launched in Job mode is going to be handled up the Job tree if it’s going to be abnormal, while the coroutine that’s launched in Job mode is going to be handled when it’s going to be abnormal and it’s going to be handled in the container. It’s a little abstract. Let’s do an example.

To demonstrate the nesting of coroutines, when there is a coroutine of Job type in the middle, if an exception occurs in the child coroutine, the exception is handed over to the root coroutine.

The display coroutine is nested and the center is decorated with a coroutine that is launched in the style of the SupervisorJob, so if something abnormal happens to the subroutine, it’s handled by the subroutine.

Answer since then, secretly thought, this next pick no fault, which know the interviewer then asked, excuse me, what is the principle? The answer, of course, is in the source code.

Code 1 is the core logic for handling the exception, which first checks the return value of cancelParent and calls handleJobException if it returns false.

The cancelParent method means that when an exception occurs at a child coroutine, it will try to cancel its parent coroutine. If it returns true, the parent coroutine will also be canceled. If it returns true, the parent coroutine will not be canceled. CancelParent may also eventually call the parent. ChildCancelled method at code 2.

The exception handling logic can be explained with an example from the workplace. Suppose the unspoken rule of the workplace is that when an employee makes a mistake, the first thing to do is to report it to your supervisor. If the supervisor is willing to deal with your mistake, the employee doesn’t have to deal with it. If the supervisor calls the problem back to the employee, the employee has to deal with it

So going back to the problem itself, a Job is a good boss, it’s willing to handle the mistakes that the subroutine makes, it’s going to be able to handle it, it’s going to be able to handle it, it’s going to be able to handle it, it’s going to be able to handle it, it’s going to be able to handle it.

The handling mechanism of coroutine exceptions is so complex that it is beyond the scope of this article, which I will discuss in another article on my knowledge planet.

5. Is that enough?

At this point, the Job and the SupervisorJob are clear. But there are many other problems, such as:

  1. CancellationException and other exceptions
  2. The difference between the coroutineScope and the container

Remember to pay attention to the “byteset” public account for more dry goods knowledge ~