https://kotlinlang.org logo
Join the conversationJoin Slack
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by Linen
coroutines
  • m

    mingkangpan

    05/20/2017, 6:52 PM
    guys I cant believe this

    https://www.youtube.com/watch?v=X1RVYt2QKQE&feature=youtu.be&t=36m54s▾

    how can they executes 100000 sleeps(1sec) in a thread pool and the whole result only takes a bit more than 1 sec?!!? what kind of magic lays behind that!? 😱
    r
    • 2
    • 7
  • c

    cedric

    05/24/2017, 1:14 AM
    @nkiesel Is this related to my
    DynamicGraph
    link on Twitter? Are you trying to run a partially ordered set in parallel?
    n
    • 2
    • 1
  • e

    elizarov

    05/24/2017, 9:09 PM
    @nkiesel sorry, not this week.. I'm on ICPC World Finals in South Dakota. Tune in to watch. Awards ceremony is going right now: http://icpclive.com
    n
    g
    • 3
    • 4
  • k

    kirillrakhman

    05/26/2017, 3:37 PM
    is firebase on the list for coroutines integrations?
    y
    • 2
    • 9
  • t

    thymecypher

    05/29/2017, 4:24 AM
    No idea -
    java.lang.NoSuchMethodError: No static method launch$default(Lkotlin/coroutines/experimental/CoroutineContext;ZLkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lkotlinx/coroutines/experimental/Job; in class Lkotlinx/coroutines/experimental/BuildersKt; or its super classes (declaration of 'kotlinx.coroutines.experimental.BuildersKt' appears in /data/app/---/base.apk)
                                                                                 at org.jetbrains.anko.sdk25.coroutines.Sdk25CoroutinesListenersWithCoroutinesKt$onClick$1.onClick(ListenersWithCoroutines.kt:284)
    e
    • 2
    • 2
  • e

    elizarov

    05/30/2017, 10:14 AM
    @deviant You ignore the resulting future (
    Deferred
    ). That is why.
    👍 1
    g
    • 2
    • 3
  • l

    louiscad

    06/01/2017, 3:12 PM
    Hi! Why doesn't kotlinx.coroutines doesn't ship with documentation and source when adding the dependency? Using Android Studio with gradle. I wanted to know the difference between
    launch(…) { … }
    and
    async(…) { … }
    , but with no doc available in the IDE, it's really inconvenient. I mean, this translates to blind autocompletion, which partly defeats it's purpose
    k
    • 2
    • 1
  • e

    elizarov

    06/03/2017, 8:51 AM
    @wouterdoeland: can you elaborate please? Does not it work or what?
    w
    o
    • 3
    • 6
  • e

    elizarov

    06/04/2017, 1:53 PM
    @nkiesel There is no need to overcomplicate
    waiFor
    just
    waitFor(predecessors: Tasks) = predecessors.forEach { it.await() }
    works just as fine
    n
    • 2
    • 11
  • w

    wouterdoeland

    06/04/2017, 4:17 PM
    When running this
    println("received update."); launch(CommonPool) { println("in pool"); //more code }
    code on my desktop, it does print
    received updated.
    and
    in pool
    , but when running it on an Digital Ocean droplet it only prints
    received update.
    . Any reason why?
    o
    e
    • 3
    • 11
  • w

    wouterdoeland

    06/04/2017, 4:20 PM
    Also, can I override functions and make them suspend (in the parent class/library they are not suspended and not in Kotlin)
    o
    • 2
    • 1
  • e

    elizarov

    06/07/2017, 5:51 AM
    @groostav Coroutines make async logic simpler to express. To get benefits over other approaches from coroutines, your code has to have some suspensions (waiting for network IO, etc). You can do all the other stuff with coroutines too by simply replacing threads with coroutines, but you will not get any saving neither in terms of code size, nor in performance. In particular, parallelising CPU-bound workflows of the type that Mike shows there falls into that category.
    g
    • 2
    • 2
  • m

    mikehearn

    06/09/2017, 11:38 AM
    ron's theory places quite some demands on the coroutine implementation. as far as i can see, kotlin's version does meet them, but i haven't looked at it extensively.
    d
    • 2
    • 2
  • e

    elizarov

    06/12/2017, 9:00 PM
    @groostav The documentation on
    isDispatchNeeded
    gives an answer: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-dispatcher/is-dispatch-needed.html There are lots of good explanations on the web as to why in JS world it is a standard to always schedule execution of async operations for later, as opposed to executing them immediately on stack (in short, it causes lots of subtle problems down the road that do not worth minor performance improvement it gives)
    g
    • 2
    • 2
  • e

    elizarov

    06/15/2017, 7:04 AM
    @groostav I can recommend this piece on why async has to be always async. It is about callbacks, but it applies to any style of async programming: https://blog.ometer.com/2011/07/24/callbacks-synchronous-and-asynchronous/
    🤔 1
    g
    • 2
    • 2
  • g

    groostav

    06/15/2017, 9:50 PM
    I'm also wondering if it might be a good idea to allow coroutines to override
    finalize()
    to
    resumeWithException
    rather than allow for the "abandoned" coroutine phenomina. I just spent a fair bit of time discovering a way we could produce abandoned coroutines and replacing them with an exception flow such that nobody simply gets left hanging (a form of live-lock I suppose?)
    r
    • 2
    • 2
  • n

    Natsuki(开元米粉实力代购)

    06/16/2017, 7:27 AM
    verticalLayout {
                button("Delay").onClick {
                    log("click begin")
                    runBlocking {
                        log("delay begin")
    //                    delay(30000L)  
                        Thread.sleep(30000L)
                        log("delay end")
                    }
                    log("click end")
                }
            }
    guys,i’m a newbee in coroutine, i tried these piece of code, when change delay to sleep, an ANR was issued by system i want to know why delay does not issue an ANR and what is main thread doing when the delay clause is executing, would anyone be kind enough to explain this for me ?
    u
    • 2
    • 5
  • v

    vaskir

    06/16/2017, 8:15 AM
    I'm not saying it's a real problem for me 🙂 However, if I needed to send many messages to a channel/actor asynchronously, setting a large channel capacity +
    offer
    would be the first thing I'd think about. I tried
    launch(parent-context) { ch.send(..) }
    , but it's very slow.
    g
    • 2
    • 6
  • e

    elizarov

    06/20/2017, 2:07 PM
    📣 Btw, video from my “Introduction to Kotlin coroutines” talk is up: https://vimeo.com/221264980/b3ac7f9001 Slides are here: https://www.slideshare.net/elizarov/introduction-to-kotlin-coroutines
    👍 9
    v
    s
    • 3
    • 2
  • e

    elizarov

    06/22/2017, 8:03 AM
    @groostav Why do you need it? Do you need the
    context
    ?
    g
    • 2
    • 1
  • v

    vaskir

    06/22/2017, 8:06 AM
    @elizarov doesn't "exceptionally" translate as "исключительно" to Russian? If so, it's not a good term.
    e
    • 2
    • 5
  • a

    alucard

    06/22/2017, 11:12 AM
    @elizarov Will the
    pipe
    function work like a mediator between the producer and consumer?
    e
    • 2
    • 1
  • e

    elizarov

    06/22/2017, 8:45 PM
    @groostav: the plan is to get rid of
    CoroutineScope
    completely and provide top-level val
    coroutineScope
    and
    coroutineScope.isActive
    extensions instead.
    g
    • 2
    • 2
  • e

    elizarov

    06/27/2017, 8:23 AM
    @louiscad I’d recommend the following workaround for your use-case in your fragment code:
    val mapAsync = async(context, CoroutineStart.LAZY) { ... your async code here to get the map ...  }
    Then you’d use it like this
    fragment.mapAsync.await()
    . It is not as clean as property delegate, but works just as fine (100% lazy and remembers the value after initialization)
    👍🏽 2
    k
    • 2
    • 3
  • e

    elizarov

    06/30/2017, 9:40 AM
    Rule of thumb: coroutines are for asynchronous tasks that wait for something most of the time. Threads are for CPU-intensive tasks.
    r
    n
    • 3
    • 11
  • g

    gildor

    07/04/2017, 5:26 PM
    Just released Kotlin Coroutines for Retrofit v0.6.0 - https://github.com/gildor/kotlin-coroutines-retrofit/releases/tag/v0.6.0 available on jcenter and maven central it’s supposed to be the last release before merge to kotlinx.coroutines integrations. If somebody has any ideas about API changes before that, please message me or PR.
    👍 3
    l
    • 2
    • 9
  • e

    elizarov

    07/05/2017, 7:23 PM
    Slides from my SPTCC 2017 (http://neerc.ifmo.ru/sptcc/) talk on (some) Lock-Free algorithms that are used in `kotlinx.coroutines`library are here https://www.slideshare.net/elizarov/lockfree-algorithms-for-kotlin-coroutines
    d
    k
    • 3
    • 4
  • r

    rockerhieu

    07/06/2017, 7:30 AM
    moved from #getting-started Any best practice to implement job queue in Kotlin using coroutines? Here is my code
    val queue = LinkedBlockingQueue<Note>()
        var closed = false
    
        init {
            async(CommonPool) {
                while (!closed) {
                    while (!closed && queue.isEmpty()) {
                        delay(50)
                    }
                    while (!closed && !queue.isEmpty()) {
                        play(queue.take())
                    }
                }
            }
        }
    I don't like
    delay(50)
    and I think it's not sufficient.
    e
    • 2
    • 3
  • s

    s1m0nw1

    07/07/2017, 11:51 AM
    Not sure if it's okay to promote personal blogs here, but I've published a post about coroutines yesterday. Maybe someone's interested :) https://blog.simon-wirtz.de/kotlin-coroutines-guide/
    v
    d
    • 3
    • 3
  • e

    elizarov

    07/10/2017, 9:49 PM
    @rogeralsing it is going to released in the next version of
    kotlinx.coroutines
    under the name of
    CompletableDeferred
    (unless we find something better) and is now available in
    develop
    branch.
    🍻 3
    r
    g
    • 3
    • 6
Powered by Linen
Title
e

elizarov

07/10/2017, 9:49 PM
@rogeralsing it is going to released in the next version of
kotlinx.coroutines
under the name of
CompletableDeferred
(unless we find something better) and is now available in
develop
branch.
🍻 3
r

rogeralsing

07/11/2017, 5:50 AM
elizarov: is there also some way to create a deferred from a result? .NET has Task.FromResult(...) making all code that awaits on such Task take the fast synchronous path
g

groostav

07/11/2017, 5:56 AM
I think I asked a similar question a while ago and I got a kind of interesting answer: coroutines deliberately dont have a "fast synchronous path", the idea basicallyb eing that if you API has aynchronous behaviour then it should either always run asynchronously or always run synchronously.
r

rogeralsing

07/11/2017, 7:07 AM
but what does that even mean? e.g. if suspend func Foo() calls suspend func Bar().. bar is just returning a value. e.g. 123... what does "run arcynchronously" mean in that context? will there be a yield upon calling Bar from Foo?
in .NET, that would have been a synchronous flow where the 123 is wrapped in a completed Task
g

groostav

07/11/2017, 5:29 PM
yeah I should say rogeralsing I've spent a fair bit of time with WPF so im pretty familiar with .net tasks --albeit when I was doing that C# 4 was new
ill see if i can dig up the article I was linked to and send it to you
View count: 6