Simple encapsulation of kotlin coroutines to help you

Briefly

After two days of learning, I have read all the classic domestic blogs and read the official documents. Recommended reading below

Good article : Getting
started with Kotlin coroutines
Official:
kotlinx.coroutines

If the English is good enough, it is recommended to see the official website, the examples are more comprehensive.

The source code address of this project

plan

After learning, in order to go further, some common APIs are specially encapsulated for other novices to learn and use. Next, the following plan functions will be completed one by one

1. Execute task1, task2 sequentially
2. Concurrently execute task1, task2
3. Wait for task2 to be executed after task1 is executed
4. Delayed execution of task1 needs to be executed after 10 seconds
5. Heartbeat execution asynchronous task executes Task at specified intervals 
6. Task The execution-aware life cycle can automatically end with the end of the life cycle of the current activity, and can configure whether to re-execute when the activity is reopened. 7. Task execution can be selected from
the main thread or sub-thread .
No network required
9. Task execution can be performed under the network standard 4G or wifi (features above 5.0)
10. Tasks can be executed while charging (features above 5.0)
11. Task execution can be canceled at any time, and all current tasks can be cancelled

At present, the framework has achieved the first seven plans, and will be gradually improved in the future.

code demo

1. Execute code sequentially

code

Effect

Sequential execution renderings

2. Execute concurrently and delay 1 second and 3 seconds respectively

code

Effect

Concurrency renderings

3. Wait for execution

code

Effect

Waiting for renderings

4. Heartbeat

code

Effect

Heartbeat renderings

5. Activity life cycle awareness

Implementation steps:

(1) Create a JobHolder to save job attributes

(2) Extend the View, add the contextJob attribute, get the Job object from the context of the View, that is, the implementation class of the JobHolder, and then extend the View's onClick event, create a coroutine in the event, and pass the Job as the context of the coroutine. Entered into the coroutine, it can be cancelled by the job

(3) Let Activity implement JobHolder, responsible for creating Job objects, and ending the Job at the end of the life cycle

JobHolder


extend()

life cycle binding

6. Switch to android UI thread in coroutine

Idea: Implemented with handler. In the coroutine, you only need to use the Handler as the context of the coroutine and get the looper of the main thread.

Code:

code

Use: pass the UI context to the coroutine

Main thread switching implementation

Summarize

The thread is the smallest unit of the process, then the coroutine is the smallest unit of the thread, and the coroutine can jump freely in different threads, and there are more advanced gameplay, the communication between the coroutines, the selection between the coroutines, the Data sharing between each other, and continue to update more advanced gameplay later.
Here is a special statement: When coroutines operate a variable at the same time, the impact of concurrency on the variable should also be considered. This is the same as multithreading, and the solution is similar, but it will not block the thread. There are many advantages to sum up, it is worth using.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325776672&siteId=291194637