Basic Coroutine Level 1
Almost every Android mobile phone works on a multiCore processor. Multithreading or concurrency is an awesome way to use a multi-core processor environment and build fast and efficient applications. The Concurrency has a direct relation with Android application Performance. Kotlin provides a cool and simple way to handle concurrency, Coroutines.In this blog series, I will try to discuss coroutines basic to advance the concept of coroutines. Get your coffee and get ready. What are coroutines?
Try this, You will see 1 Lakh coroutines run very easily. Just Try to run 10k threads :). Your program may get out of memory and your application may crash by creating 10k threads. Let’s discuss what happened behind the scene in the above program. I have used the repeat() method to create 1Lakh coroutines. Thread pool associate number of threads(for example 10 threads) to execute 1Lakhs coroutines. Each thread handles multiple coroutines, execute them, finish them, and get ready for other coroutines. Thread pool handles all the coroutines and executes them behind the scene. Awesome… We know coroutines are there to solve our two main problems for Android Developer.
We will go deeper regarding coroutines. Let us discuss some basic concepts, first. These concepts are used to create, modify, and stop coroutines. The following are some basic conceptual terms that we use in coroutines.
Fundamentals of Coroutines Scope create and run coroutines(or Start and Stop), provides lifecycle events (runBlocking is a kind of scope )
Let Discuss Scope and let’s get our hand dirty with Some Code. Global Scope: Scope of the Entire Application (Global as the name specifies )
The above example is very self-explanatory. we have created coroutine(runblocking) in the main function. runblocking will block current thread (main)and create three coroutines using launch,global-scope, and coroutine scope. global-scope for the entire application. runblocking provides coroutine on the same thread. Let’s move to the next term Context. In simple words, Context is a set of data(or variables) associated with coroutines. Scope and Context are very related to each other. Coroutine always executes in some context. Scope generates and handles the coroutine and data associated with coroutine is Context.
In the above example, we have used Dispatcher multiple times to specifies Thread on which Coroutines should work MAIN or IO. Then we use the job to check the status and cancel the job(or coroutine Scope which containing coroutines) if required. Suspend Functions are functions that can run in coroutines. We can add suspend keyword or modifier in front of the function name. Suspend Function can only be called from Suspend Function or non-suspending scope like runblocking, launch, asyn, etc. let’s take an example of suspend functions to know them more.
Let us discuss what happens in the above program. In the first coroutine, Developers and QA engineers can not work together. First Developer needs to finish the development part and then QA needs to start testing. So suspend functions getNumberQAEngineer() and getNumberDevelopers() need to work sequentially like normal functions. In the Second Coroutine, the application is published both teams can work independently. So Suspend function can work simultaneously. The First coroutine result will be
total team member 69 time taken to complete 2023 //time can vary from system to system
The Second coroutine result will be
total team member 69 time taken to complete 1076 //time can vary from system to system
Next one is JOB, A Job is a handle on coroutine. .launch() call returns job. It allows us to manipulate the coroutine lifecycle. it can access life cycle methods like cancel( ), join( ) and variable like isCancel, active. job works on a hierarchy(parent-child) level. this means jobs can have children or parents. Job is a very interesting and useful part of coroutines. An interesting fact on job cancellation nature if there is a failure in a child’s job, it will cancel the parent or the same hierarchy job. there are lots of tips and tricks on the job. let us learn a few and take the example of job
In the above example, we have created parent-job coroutine and then created 2 child-job. we easily cancel all the jobs with only canceling the parent job. we can only learn more about the jobs only with practice. I think we learn a lot lets summarise it There are more topics in the coroutine remaining exceptions, deferred, etc. For now, we have just touched the surface of the coroutines. But we have completed the most basic concepts of the coroutine. We can only expertise coroutines only by practice. So, Sky is the limit.
Android News
Our Engineering Roadmap
By Mark Ng We just completed our engineering road map for our Android apps at Australia Post. Each year we get together and try to decide on what we are going to do from an engineering perspective for the next 12 months. Each team gets to decide on what should be done now, what they want to complete by the end of the year and whats on the horizon for next year.
By
ProAndroidDev -
droidcon News
Tech Showcases, Developer Resources & Partners
EmployerBrandingHeader
jobs.droidcon.com
![]() Latest Android Jobs
Kotlin Weekly
![]() Your weekly dose of Kotlin
ProAndroidDev
![]() Android Tech Blogs, Case Studies and Step-by-Step Coding
Zalando
![]() Meet one of Berlin's top employers
Academy for App Success
![]() Google Play resources tailored for the global droidcon community |
Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.