KotlinConf 2017 - Introduction to Coroutines by Roman Elizarov

Sdílet
Vložit
  • čas přidán 2. 06. 2024
  • We live in an asynchronous era of concurrency. Modern front-end and mobile applications provide real-time feedback and communication, server-side applications and services handle thousands of online users while integrating dozens of micro-services. Old-school monolithic applications with thread-based concurrency are going out of fashion, and asynchronous styles of programming with callbacks, futures, reactive streams and coroutines are gaining popularity.
    Kotlin is a modern and pragmatic JVM language that is developed by JetBrains with a strong emphasis on Java interoperability. Due to its strong Java interop, any Java asynchronous library can be used in Kotlin. You can even use JVM-based byte-code-instrumenting coroutine implementations like Quasar and JavaFlow. So, why has Kotlin introduced coroutines as a first-class language feature and how are Kotlin coroutines different from the generate/yield and async/await coroutines in C#, Python, JS and other languages? What makes using coroutines in Kotlin different from using threads?
    This talk answers those questions and gives a gentle introduction to the world of Kotlin coroutines with live examples. You'll learn how to launch and wait for coroutine completion, what are Kotlin suspending functions and how to wrap your favorite asynchronous library into them, turning your asynchronous "callback hell" into an easy-to-read synchronous-like code. You'll see how you can write scalable applications with Kotlin coroutines to handle lots of concurrent actions (connections, users, conversations, animations, etc.) at once.
    Roman Elizarov is a professional software developer with more than 16 years experience. He started his career at Devexperts, where he designed and developed high-performance trading software for leading brokerage firms and market data delivery services that routinely handle millions of events per second. He is an expert in Java and JVM, particularly in real-time data processing, algorithms and performance optimizations for modern architectures.
    Roman currently works on the Kotlin language at JetBrains. Having worked on very large systems comprising of many modules written in different languages, he has a dream of a single language that can be used to write all parts of a very large distributed system, to share and reuse data models and algorithms without friction. Kotlin with JVM and JS backends and the upcoming native compiler is the ideal candidate to realize this dream.
    In 2000 Roman had graduated from St. Petersburg ITMO. He now teaches a course on concurrent and distributed programming in ITMO. During his undergraduate study, he participated at ACM International Collegiate Programming Contest (ICPC). Since 1997 and until now Roman serves as a Chief Judge of Northeastern European Regional Programming Contest (NEERC) of ACM ICPC.
  • Věda a technologie

Komentáře • 71

  • @igorg.8624
    @igorg.8624 Před 6 lety +94

    This is definitely a well-polished talk. It made Coroutines easy to understand, and the speaker is obviously well versed.

  • @zhou7yuan
    @zhou7yuan Před 4 lety +39

    Asynchronous programming [0:50]
    A toy problem [1:48]
    token -> post -> process
    Threads [2:54]
    (how many threads we can have?)
    Callbacks to the rescue (sort of...) [4:23]
    Callbacks: before [4:34]
    Callbacks: after [5:11]
    (aka "callback hell". exceptions is a mess...)
    Futures/Promises/Rx to the rescue (sort of...) [6:25]
    Futures: before & after [6:51]
    Futures: after (3) [7.33]
    (but all those combinators...)
    Kotlin coroutines to the rescue [9:03]
    Coroutines: before [9:12]
    Coroutines: after [10:09]
    Bonus features [11:18]
    (Regular loops, exception handing, higher-order functions)
    (Everything like in blocking code)
    Suspending functions [12:44]
    Retrofit async [13:04]
    Composition (beyond sequential) [14:43]
    Higher-order functions [15:31]
    (retryIO{})
    (suspend fun retryIO(block: suspend() -> T): T{})
    Coroutine builders [16:57]
    Coroutines revisited [17:06]
    launch (coroutine builder) [18:13]
    (Fire and forget!)
    UI Context [19:22]
    Where's the magic of launch? [19:55]
    async/await [21:07]
    Kotlin-way [21:34]
    Classic-way [21:41]
    (async/await) (C#, Python, TS, Dart, coming to JS)
    (C#) (returns a future)
    Why no await keyword in Kotlin? [23:01]
    The problem with async (default async without `await` calling)
    Kotlin suspending functions are designed to imitate sequential behavior by default [24:53]
    (Concurrency is hard, has to be explicit)
    Kotlin approach to async (Concurrency where you need it) [25:28]
    Use-case for async [25:44]
    Kotlin async function [26:40]
    (Deferred - Kotlin's future type)
    (async coroutine builder)
    (await function)
    Using async function when needed [29:10]
    (suspend fun l(); async{ l() })
    Kotlin approach to async [30:08]
    Coroutines [30:48]
    What are coroutines conceptually? [31:14]
    (very light-weight threads)
    Example [31:33]
    (runBlocking{} runs coroutine in the context of invoker thread)
    Demo [33:16]
    (Example with thread) [34:06]
    (Demo with thread) [34:50]
    (OutOfMemoryError)
    Java interop[35:54]
    (future{} coroutine builder) [37:33]
    (future1.await() - Extension for Java's CompletableFuture)
    Beyond asynchronous code [38:14]
    Fibonacci sequence [38:31]
    (buildSequence{} yield())
    (Coroutine is restricted only to suspending functions defined here)
    Synchronous [40:07]
    (Synchronous with invoker)
    Library vs Language [41:24]
    Classic async [41:37]
    Kotlin coroutines [41:54]
    Experimental status [42:41]

  • @dansadventures5514
    @dansadventures5514 Před 6 lety +33

    This was one of the best videos I've seen on coroutines as it really helped me get a sense for how I would use them in practice. Thank you!

  • @AlexGLuque
    @AlexGLuque Před 5 lety +1

    Thank you for posting this talk. It's the best initial approach I've seen so far after some weeks of reading about the coroutines. Great speaker for a great language!

  • @methodsignature
    @methodsignature Před 5 lety +4

    Excellent! This is exactly the talk I wanted. The what, the why, and the promise presented in a thoughtful and concise fashion.

  • @carrefamily01
    @carrefamily01 Před 6 lety +1

    Great presentation and a even greater product by Jetbrains! Kudos to the kotlin team! Can't wait to use coroutines in my project.

  • @alekseimulin6151
    @alekseimulin6151 Před 5 lety +8

    Jet Brains guys, you're great, I think it's a breakthrough and a way cooler concept than anything else at the moment.

  • @github.junrdev
    @github.junrdev Před 29 dny

    i watched this 6 years later and if am being real its worth 1000 videos. Really laid the basis of coroutines.

  • @ChrisAthanas
    @ChrisAthanas Před 4 lety +2

    Excellent presentation by an obvious master of his craft

  • @sleepydev4700
    @sleepydev4700 Před 4 lety

    I've seen other tutorials but this is the best explanation of them

  • @vipullal7689
    @vipullal7689 Před 3 lety +1

    Another brilliant and comprehensive talk. I now understand coroutines. Thanks a ton!

  • @saurabh75prakash
    @saurabh75prakash Před 4 lety

    This is exactly what I was looking for. Thanks!

  • @TheWitzig
    @TheWitzig Před 6 lety +1

    Such a great video! Thank you! :-)

  • @guptaanmol184
    @guptaanmol184 Před 2 lety

    Wow, way to go kotlin with saner defaults! Amazing work! ~ from a C# dev using async await :)

  • @lloydryandavid7820
    @lloydryandavid7820 Před 4 lety

    Very well explained. Thanks for the video! 👍

  • @AntonDerevyanko
    @AntonDerevyanko Před 6 lety

    Thanks for clear and understandable talk.

  • @CarlitoProductions
    @CarlitoProductions Před 6 lety

    Can you use a coroutine by not passing any arguments, and doing a while loop inside of the suspended coroutine (for example a person messes their information up during a prompt and needs to reset, so the while loop continues until they press yes)?

  • @yakiv1488
    @yakiv1488 Před 3 lety

    Thanks a lot. That's a really good talk!

  • @md.tahmidmozaffar9135
    @md.tahmidmozaffar9135 Před 4 lety

    Very good presentation.

  • @centurieswisdom
    @centurieswisdom Před 6 lety +10

    Please, fix the volume of the intro music and the volume of record itself!

  • @MarcusWolschon
    @MarcusWolschon Před 6 lety +29

    Next time: repeat the questions into a microphone!

  • @xpopcornx1747
    @xpopcornx1747 Před 4 lety +1

    C# is much more clear. When you call an async function you use await, in kotlin there is no difference between how you call a suspend function and non suspend function so there is no way of telling what the code does, you have to manually go to each function and check if it is a suspend function to know that it is waiting for something.

    • @MartinNordholts
      @MartinNordholts Před 4 lety +2

      XPopCornX This is touched upon in the talk. The solution is to have the IDE mark suspend calls in the left gutter on a line by line basis.

  • @unrealspetznaz
    @unrealspetznaz Před 3 lety +1

    really helpful

  • @pedrod.8839
    @pedrod.8839 Před 5 lety

    Where can I find the talk where he shows how coroutines are compiled in the JVM?

    • @JetBrainsTV
      @JetBrainsTV  Před 5 lety

      Is this the one you are looking for? czcams.com/video/YrrUCSi72E8/video.html

  • @ColinTheShots
    @ColinTheShots Před 6 lety +2

    Will the slides be shared? Thanks!

    • @JetBrainsTV
      @JetBrainsTV  Před 6 lety +4

      www.slideshare.net/elizarov/introduction-to-coroutines-kotlinconf-2017

    • @ColinTheShots
      @ColinTheShots Před 6 lety

      Thanks!

  • @Rjbcc
    @Rjbcc Před 5 lety

    I must be missing something very basic here but what is the point of making the first example asynchronous? If we need to create a `Post` before we can process it, and we need to create a `Token` before we can create a `Post`, isn't this a set of synchronous operations? What will actually be done asynchronously? Are we saying we want the entire operation of create/post/process to be async while the app does something else?

    • @GooksanGom
      @GooksanGom Před 3 lety

      UI interactions I think

    • @Rjbcc
      @Rjbcc Před 6 měsíci

      Back to answer my own question 4 years later. Yes the Post example consists of sequential operations that are not executed concurrently, the whole operation can be launched asynchronously inside of a coroutine, and not be thread bound at any IO suspension point. The async part is the whole operation, and the suspending operations allow us to free up threads to do other work instead of blocking on network IO.

  • @vulnerable_content
    @vulnerable_content Před 2 lety

    Thank you!

  • @tahirraza2590
    @tahirraza2590 Před rokem

    A bit old but still related and on-point. Really liked the way he presented the whole thing as a story. The what, they why and the how.

  • @rckd5903
    @rckd5903 Před 5 lety +2

    wow, kotlin has async/await too :-D

    • @houssemzaier
      @houssemzaier Před 3 lety

      They are in higher lever when you compare them to other languages. async and await are functions not keywords. And this is way better! If you understand

  • @kc2815
    @kc2815 Před 6 lety +2

    Wait wait wait... So in the code where he has the two calls to loadImageAsync() and then calls await on the two deferred objects one after the other, is that really asynchronous? Wouldn't the first call suspend until it was completed and then call the second one? Or does the loading happen when the function is invoked and then the await call only makes sure the call completes before moving to the next line?

    • @henninghoefer
      @henninghoefer Před 6 lety +3

      Kevin Colin Both images are loaded asynchronously at virtually the same time, only the await() are blocking and ensure both images are there before combining them.

    • @crazieeez
      @crazieeez Před 6 lety

      When you are confuse, the programming language is garbage. Coroutine is garbage.

    • @user-do4oh6rw9u
      @user-do4oh6rw9u Před 5 lety +2

      so RX Java is garbage too, because everyone is confused firstly)

  • @a0um
    @a0um Před 5 lety

    I've struggled to hear the speaker even maxing out my phone volume.
    I found the example contrived: what's the point of making those functions async?
    I agree the "suspend" keyword isn't the one I would have expected.
    This is my first exposure to coroutines and I'm gonna leave half way. Still exited about Kotlin though, and I'll be looking for other introductions to coroutines.

  • @robchr
    @robchr Před 6 lety +6

    So coroutines are just monads where suspend functions are Kleisli arrows, '=' is 'flatMap' and 'await' is 'pure'. Got it ;-)

    • @methodsignature
      @methodsignature Před 5 lety +1

      Us functional casuals needed all the other words in between.

  • @BradMcHelm
    @BradMcHelm Před 6 lety

    please check out the flashing icons, giving me epilepsy

  • @hagbardceline9866
    @hagbardceline9866 Před 6 lety +2

    Interesting talk! But i don't really see the improvement from the first - rightfully - criticized approach of having "to many" combinators to "remember" in the promise/future/Rx case when in fact i have the same thing with the so called coroutine builders. This is not really compelling, at least the talk does not make it clear to me why i should trade like 3 combinators with 3 coroutine builders ( i oversimplified this here )

    • @vangrails
      @vangrails Před 6 lety +1

      Maybe the handling of exceptions is easier?

  • @garywzh
    @garywzh Před 6 lety

    great talk

  • @mayureshgharat1600
    @mayureshgharat1600 Před 2 lety

    I am wondering how is the launch function at 20:41 different the previous retryIO function. Why does retryIO need to be a suspend function?

  • @abunapha
    @abunapha Před 5 lety +1

    The generated subtitles are hilarious

    • @JetBrainsTV
      @JetBrainsTV  Před 5 lety +1

      Any examples to make us smile?

    • @abunapha
      @abunapha Před 5 lety +3

      @@JetBrainsTV 19:24 "lunch curtain builder"
      Also Kotlin is always "catalan"

    • @JetBrainsTV
      @JetBrainsTV  Před 5 lety +1

      @@abunapha "lunch curtain builder" - That's hilarious!

  • @SpectatorAlius
    @SpectatorAlius Před 3 lety

    He keeps pronouncing 'coroutine' as 'car routine' -- and I finally guessed why! He must have learned Russian in Moscow or somewhere else nearby with a pronounced акане accent!

  • @kristofs8893
    @kristofs8893 Před 2 lety

    Kotlin is very good from programming, way quicker than Java. I studied Java at Uni in London, but still there is a lot still to be learnt. Very interesting the fact that Kotlin is built on top of JVM.

  • @ybtoo7
    @ybtoo7 Před 3 lety

    Everything is good except for speakers English accent. Its been tough to follow here and there due to lack of clean pronunciation, but can figure out of the context. Should have provided properly generatedsubtitles. Except this rest is wonderful.

  • @dnkilic
    @dnkilic Před 4 lety

    More recent one is here; czcams.com/video/hb0hfHVWCS0/video.html

  • @miracledoh4020
    @miracledoh4020 Před 3 lety

    comparing coroutine with Thread is pointless, coroutine's java equivalent should be Executor

  • @kemuri22
    @kemuri22 Před 6 lety

    kinda like apple's gcd

  • @StefanReich
    @StefanReich Před 5 lety

    Yeah it's a good idea (coroutines). I also started adding them to Java at some point... didn't complete the project yet, but it may be to come. dzone.com/articles/java-can-have-coroutines

  • @aprofromuk
    @aprofromuk Před 5 lety

    cheesiest crapiest startest music ever :)

  • @typingcat
    @typingcat Před 2 lety +1

    Stop starting a lecture video with a useless loud long music sequence.l

  • @crazieeez
    @crazieeez Před 6 lety +5

    coroutines look like garbage. the syntax is incredibly complicated to try to simplify async operation. Get your naming right and stop with a bunch of non alphabet characters to confuse people. The explanation given in this video is garbage. Author tries to use adjective to express how kotlin coroutines are better than async/await and how much simpler ... I don't buy it. suspend is a dumb name to give to a function for async operation ... suspend means don't run the function.

    • @rckd5903
      @rckd5903 Před 5 lety

      crazieeez exactly what i thought 😂

    • @valour.se47
      @valour.se47 Před 5 lety +2

      Easy man 👨

    • @davidkerr7
      @davidkerr7 Před 5 lety +1

      I think it because JavaScript already has Async and wait keywords