Skip to main content
Browse topics

Parallel Decomposition with async in Kotlin

2 min read Updated June 30, 2026
Share

Introduction

Parallel Decomposition with async is a fundamental concept every Kotlin developer should understand. async starts parallel work and await retrieves its result. Together they compose concurrent operations without manual thread management.

Multiple async blocks can run concurrently inside same scope. In this tutorial you will learn the syntax, walk through a complete example program, study the sample output, and review best practices so you can apply the concept confidently in your own projects.

Definition

  • Multiple async blocks can run concurrently inside same scope.
  • await collects results after parallel execution.
  • Reduces total waiting time for independent tasks.

Syntax

kotlin
async { task1() }
async { task2() }

Parallel Decomposition with async in Kotlin Example Program in Kotlin

kotlin
import kotlinx.coroutines.*

fun main(args: Array<String>) = runBlocking {
    val a = async { delay(100); 10 }
    val b = async { delay(100); 20 }
    println("Sum = ${a.await() + b.await()}")
}

Sample Output

plaintext
Sum = 30

When to use

Use async/await when you have independent tasks that can run in parallel and you need to combine their results.

How it works

  1. The program starts with a main function — the entry point that runs when you execute the file.

  2. val a = async { delay(100); 10 } assigns or updates a value used later in the program.

  3. val b = async { delay(100); 20 } assigns or updates a value used later in the program.

  4. The println("Sum = ${a.await() + b.await()}") statement writes a line to the console — this produces part of the sample output below.

  5. Multiple async blocks can run concurrently inside same scope.

  6. Run the program in IntelliJ IDEA, Android Studio, or with the Kotlin command-line compiler (kotlinc / kotlin). Compare your console output with the sample output shown below.

Best Practices

Common Mistakes

Key Points

  • Multiple async blocks can run concurrently inside same scope.
  • await collects results after parallel execution.
  • Reduces total waiting time for independent tasks.
  • Test the example locally and verify the output matches the sample.
  • Experiment by changing input values to see how behaviour changes.

Notes

  • Add the kotlinx-coroutines-core dependency when running coroutine examples outside Android or IntelliJ.
  • Semicolons at the end of statements are optional in Kotlin.

Frequently Asked Questions

What is Parallel Decomposition with async in Kotlin?
Multiple async blocks can run concurrently inside same scope.
When should I use Parallel Decomposition with async?
Use async/await when you have independent tasks that can run in parallel and you need to combine their results.
How is Parallel Decomposition with async different from Java?
Reduces total waiting time for independent tasks.
How do I practice this topic?
Copy the example program into IntelliJ IDEA or Android Studio, run it, then modify values or add print statements to confirm your understanding.

Related tutorials

Search tutorials