-
Notifications
You must be signed in to change notification settings - Fork 1.9k
ChannelIterator: add an example #4539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1050,7 +1050,36 @@ public inline fun <T> ChannelResult<T>.onClosed(action: (exception: Throwable?) | |
|
||
/** | ||
* Iterator for a [ReceiveChannel]. | ||
* Instances of this interface are *not thread-safe* and shall not be used from concurrent coroutines. | ||
* Instances of this interface are *not thread-safe*. | ||
* Each iterator instance should be used by a single coroutine. | ||
* | ||
* An example usage: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you describe the sequence of events where this sample would answer a question a user may realistically have? It neither explains the |
||
* | ||
* ``` | ||
* val channel = Channel<Int>() | ||
* launch { | ||
* channel.send(1) | ||
* channel.send(2) | ||
* channel.send(3) | ||
* channel.close() // NB: must close for iterators to finish | ||
* } | ||
* launch { | ||
* for (element in channel) { | ||
* println("Consumer A got $element") | ||
* } | ||
* } | ||
* launch { | ||
* for (element in channel) { | ||
* println("Consumer B got $element") | ||
* } | ||
* } | ||
* ``` | ||
* Possible output: | ||
* ```text | ||
* Consumer A got 1 | ||
* Consumer A got 2 | ||
* Consumer B got 3 | ||
* ``` | ||
*/ | ||
public interface ChannelIterator<out E> { | ||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not true, it can be passed around different coroutines as long as this doesn't result in multithreaded access.