Day86 of #100DaysOfCode
Hii folks 🙌
Today I will be starting a new unit and pathway in which we will learn Multithreading and concurrency in Android.
Unit 4: Internet
Pathway 1: Coroutines
https://developer.android.com/courses/android-basics-kotlin/course
Multithreading and concurrency
So far we have treated an Android app as a program with a single path of execution. We can do a lot with that single path of execution, but as our app grows, we need to think about concurrency.
Concurrency allows multiple units of code to execute out of order or seemingly in parallel permitting more efficient use of resources. The operating system can use characteristics of the system, programming language, and concurrency unit to manage to multitask.
Why do we need to use concurrency? As our app gets more complex, it’s important for our code to be non-blocking. This means that performing a long-running task, such as a network request, won’t stop the execution of other things in our app. Not properly implementing concurrency can make our app appear unresponsive to users.
A thread is the smallest unit of code that can be scheduled and run in the confines of a program. Here’s a small example where we can run concurrent code.
We can create a simple thread by providing a lambda. Try the following in the playground.
fun main() {
val thread = Thread {
println("${Thread.currentThread()} has run.")
}
thread.start()
}
The thread isn’t executed until the function reaches the start()
function call. The output should look something like this.
Thread[Thread-0,5,main] has run.
Note that currentThread()
returns a Thread
instance which is converted to its string representation which returns the thread's name, priority, and thread group. The output above might be slightly different.
Creating and running multiple threads
To demonstrate simple concurrency, let’s create a couple of threads to execute. The code will create 3 threads printing the information line from the previous example.
fun main() {
val states = arrayOf("Starting", "Doing Task 1", "Doing Task 2", "Ending")
repeat(3) {
Thread {
println("${Thread.currentThread()} has started")
for (i in states) {
println("${Thread.currentThread()} - $i")
Thread.sleep(50)
}
}.start()
}
}
Output in playground:
Thread[Thread-2,5,main] has started Thread[Thread-2,5,main] - Starting Thread[Thread-0,5,main] - Doing Task 1 Thread[Thread-1,5,main] - Doing Task 1 Thread[Thread-2,5,main] - Doing Task 1 Thread[Thread-0,5,main] - Doing Task 2 Thread[Thread-1,5,main] - Doing Task 2 Thread[Thread-2,5,main] - Doing Task 2 Thread[Thread-0,5,main] - Ending Thread[Thread-2,5,main] - Ending Thread[Thread-1,5,main] - Ending Thread[Thread-0,5,main] has started
Thread[Thread-0,5,main] - Starting
Thread[Thread-1,5,main] has started
Thread[Thread-1,5,main] - Starting
Output in AS(console):
Thread[Thread-0,5,main] has started
Thread[Thread-1,5,main] has started
Thread[Thread-2,5,main] has started
Thread[Thread-1,5,main] - Starting
Thread[Thread-0,5,main] - Starting
Thread[Thread-2,5,main] - Starting
Thread[Thread-1,5,main] - Doing Task 1
Thread[Thread-0,5,main] - Doing Task 1
Thread[Thread-2,5,main] - Doing Task 1
Thread[Thread-0,5,main] - Doing Task 2
Thread[Thread-1,5,main] - Doing Task 2
Thread[Thread-2,5,main] - Doing Task 2
Thread[Thread-0,5,main] - Ending
Thread[Thread-2,5,main] - Ending
Thread[Thread-1,5,main] - Ending
We will run the code several times. We’ll see the varied output. Sometimes the threads will appear to run in sequence and other times the content will be interspersed.
That is all for Day86 ✅
Thanks for reading, See you tomorrow!