Code Monkey home page Code Monkey logo

coroutinesbasic's Introduction

CoroutinesBasic

Introduction to Coroutines on Android and Structured Concurency

Recently, Kotlin is widely adopted by Android Developers. Besides the clean and concise syntax, one of the most important features was introduced by Kotlin is Coroutine.

This topic intends to introduce you to how Kotlin Coroutines on Android solve two primary problems (long-running tasks management and main-safety) and how it makes the concurrency code in Kotlin easier to read, write, and understand.

Furthermore, you will know how Structured Concurrency helps you keep track of the work that's being done.

View the topic's slides

The first example was created to demonstrate: Coroutines are like "light-weight" threads ThreadAndCoroutinesFragment.kt

     /**
     * Create 10k coroutines and each one print a number after 0.1 second delay
     */
    private fun coroutines() = runBlocking {
        val measureTimeMillis = measureTimeMillis {

            val jobs = List(10_000) {number->
                launch {
                    delay(100)
                    println(number)
                }
            }
            jobs.forEach { it.join() }
        }

        timeDisplayTv.text = "$measureTimeMillis ms"
        println("Total Time = $measureTimeMillis")

    }


    /**
     * Create 10k threads and each one print a number after 0.1 second delay
     */
    private fun threads() = runBlocking {
        val measureTimeMillis = measureTimeMillis {
            val jobs = List(10_000) {number ->
                thread {
                    Thread.sleep(100)
                    println(number)
                }
            }

            jobs.forEach { it.join() }
        }
        timeDisplayTv.text = "$measureTimeMillis ms"
        println("Total Time = $measureTimeMillis")
    }

The second example was created to demonstrate: Suspend and resume work together to replace the callback DocViewModel.kt

    fun userNeedDoc() {
        viewModelScope.launch(coroutineExceptionHandler) {
            fetchDoc()
        }
    }

    private suspend fun fetchDoc() {
        coroutineScope {
            val doc = get("https://developer.android.com")
            docLiveData.postValue(doc)
        }
    }

    private suspend fun get(url: String): String = withContext(Dispatchers.IO) {
        val deferred = network.getDocsWithCoroutineAsync(url)
        deferred.await()
    }

coroutinesbasic's People

Contributors

taindb avatar

Stargazers

Anh avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.