Blog Infos
Author
Published
Topics
, , , ,
Published

In concurrent programming, managing access to shared resources is crucial, especially in multi-threaded environments like Android. A common challenge developers face is the risk of race conditions, where multiple threads or coroutines access shared data concurrently, leading to unpredictable and incorrect results. One of the most effective solutions to this problem is using a Mutex (short for Mutual Exclusion). In this blog, we’ll explore what Mutex is, how it works, and its applications in Android with practical Kotlin examples to demonstrate how it helps prevent race conditions.

What is a Mutex?

Mutex, or Mutual Exclusion, is a synchronization primitive that helps ensure that only one thread or coroutine can access a critical section of code at a time. In simpler terms, a Mutex allows a thread or coroutine to “lock” a resource, ensuring exclusive access until the thread or coroutine finishes its task and releases the lock. This mechanism prevents other threads or coroutines from entering the critical section until the lock is released, effectively avoiding race conditions.

Why Use Mutex in Android?

In Android development, especially in multi-threaded applications, managing shared resources like memory, files, or variables can become problematic. For example, accessing or modifying a shared variable from multiple threads without synchronization can lead to inconsistent data, crashes, or unexpected behavior. Mutexes are used to synchronize access to shared resources, ensuring data integrity and consistency.

Race Conditions Explained

A race condition occurs when two or more threads or coroutines try to access shared resources simultaneously, and the final outcome depends on the timing or sequence of execution.

Race Condition Example : Updating a Shared List

In this example, we aim to demonstrate a common concurrency issue known as a race condition. The provided code involves multiple coroutines adding items to a shared list. Due to the concurrent nature of coroutines running on different threads, we encounter a race condition where the expected behavior—adding 2000 items to the list—may not be met. Instead, due to simultaneous access and modification of the shared list, the actual number of items may be less than 2000.

import kotlinx.coroutines.*

val sharedList = mutableListOf<String>()

suspend fun addItem(item: String) {
    // Artificial delay to simulate a longer processing time
    delay(1)
    sharedList.add(item)
}

fun main() = runBlocking {
    val startTime = System.currentTimeMillis()

    // Use Dispatchers.Default to ensure coroutines run on different threads
    val job1 = launch(Dispatchers.Default) { repeat(1000) { addItem("A") } }
    val job2 = launch(Dispatchers.Default) { repeat(1000) { addItem("B") } }

    job1.join()
    job2.join()

    val endTime = System.currentTimeMillis()
    val elapsedTime = endTime - startTime

    println("List size: ${sharedList.size}") // Expected: 2000, Actual: may vary due to race condition
    println("Elapsed time: $elapsedTime ms")
}

ouput

List size: 1987
Elapsed time: 1273 ms
Explanation :
  • sharedList: A mutable list shared between coroutines.
  • addItem(item: String): A suspend function simulating a delay before adding an item to sharedList.
  • job1 and job2: Coroutines launched to concurrently add items “A” and “B” to sharedList, respectively.
  • startTime and endTime: Used to measure the elapsed time of the operation.
How Race Condition Occurs:
  • Concurrent Modifications: Both job1 and job2 are running concurrently on different threads, attempting to modify sharedList simultaneously.
  • Delayed Operation: The delay introduced by delay(1) simulates processing time, making it possible for both coroutines to be operating on the list at the same time.
  • Inconsistent State: Without synchronization, simultaneous access and modifications to sharedList can lead to inconsistent states, such as overwriting each other’s changes or missing some additions.

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

,

Jetpack Compose: Drawing without pain and recomposition

This is a talk on recomposition in Jetpack Compose and the myths of too many calls it is followed by. I’ll briefly explain the reasons behind recompositions and why they are not as problematic as…
Watch Video

Jetpack Compose: Drawing without pain and recomposition

Vitalii Markus
Android Engineer
Flo Health Inc.

Jetpack Compose: Drawing without pain and recomposition

Vitalii Markus
Android Engineer
Flo Health Inc.

Jetpack Compose: Drawing without pain and recomposition

Vitalii Markus
Android Engineer
Flo Health Inc.

Jobs

How Mutex Fixes the Problem

To solve the race condition, we can use a Mutex to ensure that only one coroutine can modify the sharedList at a time. This approach provides mutual exclusion, preventing simultaneous modifications.

import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock

val sharedList = mutableListOf<String>()
val mutex = Mutex()

suspend fun addItem(item: String) {
    // Artificial delay to simulate a longer processing time
    delay(1)
    mutex.withLock {
        sharedList.add(item)
    }
}

fun main() = runBlocking {
    val startTime = System.currentTimeMillis()

    val job1 = launch(Dispatchers.Default) { repeat(1000) { addItem("A") } }
    val job2 = launch(Dispatchers.Default) { repeat(1000) { addItem("B") } }

    job1.join()
    job2.join()

    val endTime = System.currentTimeMillis()
    val elapsedTime = endTime - startTime

    println("List size: ${sharedList.size}") // Expected: 2000
    println("Elapsed time: $elapsedTime ms")
}

List size: 2000
Elapsed time: 1300 ms
Explanation:
  • Mutex Declaration: val mutex = Mutex() creates a Mutex instance to handle synchronization.
  • Locking the Critical Section: In the addItem function, mutex.withLock ensures that only one coroutine at a time can enter the critical section where sharedList is modified.
  • Consistent State: With the Mutex in place, concurrent modifications to sharedList are prevented, ensuring that the size of the list matches the expected 2000 items.
Performance Comparison
  • Without Mutex: The list size may be inconsistent, and the execution time might be affected by race conditions.
  • With Mutex: The list size will consistently be 2000, and while Mutex introduces some overhead due to locking, it ensures data consistency.
Best Practices for Using Mutex in Android
  • Avoid Overusing Mutex: Excessive locking can lead to performance issues, like deadlocks or reduced app responsiveness. Use Mutexes only where necessary.
  • Release the Lock Promptly: Ensure that the Mutex is released as soon as the critical section is complete to avoid blocking other threads.
  • Combine with Coroutines: Kotlin’s coroutines work seamlessly with Mutexes, making it easy to handle asynchronous tasks without traditional thread overhead.
Conclusion

Mutex is a powerful tool for handling concurrency in Android applications, especially when dealing with shared resources. By using Mutex, developers can prevent race conditions, ensuring data consistency and application stability. Understanding when and how to use Mutex can greatly improve your app’s performance and reliability, making your multi-threaded code safer and more predictable.

With practical applications in industries like finance, gaming, and real-time processing, Mutex is an essential concept for any Android developer dealing with concurrency. Remember to use it wisely, keeping performance considerations in mind, and you’ll be well-equipped to handle multi-threaded challenges in your Android projects.

This article is previously published on proandroiddev.com

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
In this part of our series on introducing Jetpack Compose into an existing project,…
READ MORE
blog
In the world of Jetpack Compose, where designing reusable and customizable UI components is…
READ MORE
blog

How to animate BottomSheet content using Jetpack Compose

Early this year I started a new pet project for listening to random radio…
READ MORE
Menu