Then how to come back into the while loop?
# coroutines
s
Then how to come back into the while loop?
g
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
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
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
If omit
delay
and every
getElement
returns null, this will cause an infinite cycle and make the cpu usage to 100%
g
getElement shouldn’t return null, but suspend until element is available
s
oh, I got it.