Coder Social home page Coder Social logo

coroutinesexample's Introduction

CoroutinesExample

How Suspending functions works

Let's say we have a function named myFunction.

fun myFunction(){

Code block 1

Code block 2 //this one has a long running operation

Code block 3

Code block 4

}

Usually these code blocks execute like block1, block2, block3, block4 . So code block 3 and 4 might execute while code block 2 is still running. (synchronously) Because of that reason there can be problems. (screen might freeze, app might crash)

(assume code block 3 is a click event code of a button,..)

But if we make this function suspend.

suspend fun MyFunction(){

Code block 1

Code block 2 //this one has a long running operation

Code block 3

Code block 4

}

Now, this function can get paused when code block 2(long running operation) starts executing and get resumed when it is done. Code block 3 and 4 will execute after that. So there will be no unexpected thread sharing issues.

Functions marked with the suspend keyword are transformed at compile time to be made asynchronous under the hood, even though they appear synchronous in the source code.

A suspending function doesn't block a thread,

but only suspends the coroutine itself. (one thread can have more coroutines)

The thread is returned to the pool while the coroutine is waiting,

and when the waiting is done, the coroutine resumes on a free thread in the pool.

coroutinesexample's People

Contributors

aldikitta 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.