Day86 of #100DaysOfCode

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!

--

--

--

https://www.linkedin.com/in/itskushagra

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A New Englander’s Guide to Picking Up Tech Debt

Altoros Appoints Tony Hansmann as New CTO of Cloud Orchestration

How to Enhance Your Windows Batch Files by Adding GUI

Safari Push Notifications: Complete Setup

DevOps Adoption in Geographically Dispersed Organizations

Contribution to the tech community

Decision Fatigue: Don’t Squeeze Out Every Bit of Your Attention

graphic showing four symptoms of decision fatigue

Apache Maven Interview Questions and Answers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kushagra Kesav

Kushagra Kesav

https://www.linkedin.com/in/itskushagra

More from Medium

Day65 of #100DaysOfCode

Day69 of #100DaysOfCode

Day61 of #100DaysOfCode

Day71 of #100DaysOfCode