Skip to content

This project demonstrates various concurrency techniques in Kotlin using coroutines. The examples include fetching and processing data concurrently, handling errors, and performing complex tasks.

Notifications You must be signed in to change notification settings

birdeveloper/kotlin_concurrency_project

Repository files navigation

Kotlin Concurrency Example Project

This project demonstrates various concurrency techniques in Kotlin using coroutines. The examples include fetching and processing data concurrently, handling errors, and performing complex tasks.

Table of Contents

Introduction

Concurrency is the ability of a program to run multiple threads at the same time. This is important for handling large and complex operations more efficiently. This project explores concurrency in Kotlin and demonstrates how to manage it using clean code standards.

Getting Started

To get started with this project, clone the repository and build the project using Gradle.

git clone https://github.com/yourusername/kotlin_concurrency_example.git
cd kotlin_concurrency_example
./gradlew build

Examples

Main.kt

This file serves as the entry point of the project. It initializes the `DataFetcher` and `DataProcessor` classes and demonstrates various concurrency techniques.

fun main() = runBlocking {
    println("Starting the concurrency example project...")
    val time = measureTimeMillis {
        val fetcher = DataFetcher()
        val processor = DataProcessor()

        val data = fetcher.fetchData()

        val job1 = launch { processor.processData(data, 1) }
        val job2 = launch { processor.processData(data, 2) }
        val job3 = launch { processor.processData(data, 3) }
        
        joinAll(job1, job2, job3)
    }
    println("Completed in $time ms")
}

Worker.kt

This file defines the `Worker` class, which performs various tasks concurrently.

class Worker(private val id: Int) {
    suspend fun performTask(taskName: String) {
        println("Worker \$id is performing task: \$taskName")
        delay(1000L)
        println("Worker \$id completed task: \$taskName")
    }
}

DataFetcher.kt

This file defines the `DataFetcher` class, which simulates fetching data from different sources.

class DataFetcher {
    suspend fun fetchData(): List<String> = withContext(Dispatchers.IO) {
        val deferredData = (1..5).map { 
            async {
                delay(500L)
                "Data \$it"
            }
        }
        deferredData.awaitAll()
    }
}

DataProcessor.kt

This file defines the `DataProcessor` class, which processes the fetched data.

class DataProcessor {
    suspend fun processData(data: List<String>, workerId: Int) {
        for (item in data) {
            println("Worker \$workerId is processing: \$item")
            delay(200L)
        }
        println("Worker \$workerId completed processing.")
    }
}

Utils.kt

This file contains utility functions used throughout the project.

object Utils {
    fun printMessage(message: String) {
        println("Utils: \$message")
    }
}

Running Tests

To run the tests for this project, use the following command:

./gradlew test

References

For a detailed explanation of the concepts demonstrated in this project, check out my article: Mastering Concurrency in Kotlin with Clean Code Principles.


Feel free to explore the code and reach out if you have any questions or suggestions.

About

This project demonstrates various concurrency techniques in Kotlin using coroutines. The examples include fetching and processing data concurrently, handling errors, and performing complex tasks.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages