https://kotlinlang.org logo
#coroutines
Title
# coroutines
s

sailxjx

11/15/2017, 8:54 AM
Then how to come back into the while loop?
g

gildor

11/15/2017, 8:56 AM
Yes, something like that come back?
oh, got it
just
Copy code
while (true) {
    if (queue.size < 10000) {
       queue.add(getElement()) // getElement can be suspend function that gets element asynchronously
    }  else {
      delay(QUEUE_DELAY) // Optional delay between retry, but probably don't need it
   }
}
s

sailxjx

11/15/2017, 9:02 AM
So these is still a
delay
in the while loop, and this code block should put into a suspend function. Is this means I can only use
sleep
in the place of
delay
without coroutine?
These is an issue in the kafka channel, the only solution seems to be sleep. https://issues.apache.org/jira/browse/KAFKA-3159
g

gildor

11/15/2017, 9:07 AM
I don’t think that it’s correct way in terms of architecture to move delay to
getElement
Again, you can just omit delay
s

sailxjx

11/15/2017, 9:11 AM
If omit
delay
and every
getElement
returns null, this will cause an infinite cycle and make the cpu usage to 100%
g

gildor

11/15/2017, 9:13 AM
getElement shouldn’t return null, but suspend until element is available
s

sailxjx

11/15/2017, 9:16 AM
oh, I got it.
2 Views