Giter VIP home page Giter VIP logo

docs's Introduction

SpringBootCoroutines

GlobalScope

  • You can use GlobalScope to start a coroutine at the top level of your application. This is often discouraged because it can lead to unmanaged coroutines that outlive the application's lifecycle.
    GlobalScope.launch { /* code to run in a coroutine */ }

CoroutineScope

  • You can create your own CoroutineScope and launch coroutines within it.This is the recommended way to start coroutines especially when working within a structured component like a ViewModel or a service .
val myScope = CoroutineScope(Dispatchers.Default)
myScope.launch {}
myScope.async {}
myScope.async {}.await()
myScope.withContext {}

async

  • The async builder is used to start a coroutine that computes a result asynchronously and returns a Deferred object. You can use await() on the Deferred object to retrieve the result .
val result = async { /* asynchronous computation */ }.await()

withContext

  • You can use the withContext builder to switch to a different coroutine context temporarily for a block of code. This is commonly used for switching between dispatchers for specific tasks.
val result = withContext(Dispatchers.Default) { /* code to run in Default dispatcher */ }

runBlocking

  • The runBlocking builder is used to start a new coroutine and block the current thread until the coroutine completes. It's typically used in testing or in the main function of a Kotlin program.
runBlocking {/* code to run in a new coroutine */ }

runCatching

  • runCatching is an extension function available in the kotlinx.coroutines library that allows you to launch a coroutine while capturing and handling exceptions that may occur within the coroutine. It returns a CancellableContinuation object that can be used to handle the result or exception.
val job = GlobalScope.launchCatching {
    // Coroutine code that may throw an exception
}

job.invokeOnCompletion { result ->
    if (result.isFailure) {
        val exception = result.exceptionOrNull()
        // Handle the exception here
    }
}

Closeable

use function

  • The use function is an extension function that comes from the Closeable interface. It is used for managing resources that need to be closed when they are no longer needed.
  • Use function used because consumer need to be closed after use landsEventConsumer.close()
landsEventConsumer.use { landsEventConsumer ->
    val job = launch {
        while (isActive) {
            val records = landsEventConsumer.poll(pollTimeout)
            for (record in records) {
                val landResponse = record.value()
                lands.addAll(landResponse.lands ?: emptyList())
            }
        }
    }

    job.join()
}

Join function

  • The job.join() call is used to wait for the completion of a coroutine represented by the job object.
  • Function join() that can be called on a Job object returned by launch. It blocks the current thread until the coroutine represented by the Job completes.

docs's People

Contributors

ahmedgodaa avatar

Watchers

 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.