1- <!-- -
2-
3- Copyright 2016-2017 JetBrains s.r.o.
4-
5- Licensed under the Apache License, Version 2.0 (the "License");
6- you may not use this file except in compliance with the License.
7- You may obtain a copy of the License at
8-
9- http://www.apache.org/licenses/LICENSE-2.0
1+ <!-- - INCLUDE .*/example-([a-z]+)-([0-9]+)\.kt
2+ /*
3+ * Copyright 2016-2017 JetBrains s.r.o.
4+ *
5+ * Licensed under the Apache License, Version 2.0 (the "License");
6+ * you may not use this file except in compliance with the License.
7+ * You may obtain a copy of the License at
8+ *
9+ * http://www.apache.org/licenses/LICENSE-2.0
10+ *
11+ * Unless required by applicable law or agreed to in writing, software
12+ * distributed under the License is distributed on an "AS IS" BASIS,
13+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ * See the License for the specific language governing permissions and
15+ * limitations under the License.
16+ */
1017
11- Unless required by applicable law or agreed to in writing, software
12- distributed under the License is distributed on an "AS IS" BASIS,
13- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14- See the License for the specific language governing permissions and
15- limitations under the License.
18+ // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit.
19+ package guide.$$1.example$$2
1620
21+ import kotlinx.coroutines.experimental.*
1722-->
23+ <!-- - KNIT kotlinx-coroutines-core/src/test/kotlin/guide/.*\.kt -->
1824
1925# Guide to kotlinx.coroutines by example
2026
@@ -63,31 +69,8 @@ This is a short guide on core features of `kotlinx.coroutines` with a series of
6369 * [ Fan-in] ( #fan-in )
6470 * [ Buffered channels] ( #buffered-channels )
6571
66- <!-- - KNIT kotlinx-coroutines-core/src/test/kotlin/guide/.*\.kt -->
67-
68- <!-- - INCLUDE .*/example-([a-z]+)-([0-9]+)\.kt
69- /*
70- * Copyright 2016-2017 JetBrains s.r.o.
71- *
72- * Licensed under the Apache License, Version 2.0 (the "License");
73- * you may not use this file except in compliance with the License.
74- * You may obtain a copy of the License at
75- *
76- * http://www.apache.org/licenses/LICENSE-2.0
77- *
78- * Unless required by applicable law or agreed to in writing, software
79- * distributed under the License is distributed on an "AS IS" BASIS,
80- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
81- * See the License for the specific language governing permissions and
82- * limitations under the License.
83- */
84-
85- // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit.
86- package guide.$$1.example$$2
72+ <!-- - END_TOC -->
8773
88- import kotlinx.coroutines.experimental.*
89- -->
90-
9174## Coroutine basics
9275
9376This section covers basic coroutine concepts.
@@ -1060,13 +1043,15 @@ fun main(args: Array<String>) = runBlocking<Unit> {
10601043
10611044### Building channel producers
10621045
1063- The pattern where a coroutine is producing a sequence of elements into a channel is quite common.
1046+ The pattern where a coroutine is producing a sequence of elements is quite common.
1047+ This is a part of _ producer-consumer_ pattern that is often found in concurrent code.
10641048You could abstract such a producer into a function that takes channel as its parameter, but this goes contrary
1065- to common sense that results must be returned from functions. Here is a convenience
1066- coroutine builder named [ buildChannel] that makes it easy to do it right:
1049+ to common sense that results must be returned from functions.
1050+
1051+ There is a convenience coroutine builder named [ produce] that makes it easy to do it right:
10671052
10681053``` kotlin
1069- fun produceSquares () = buildChannel <Int >(CommonPool ) {
1054+ fun produceSquares () = produce <Int >(CommonPool ) {
10701055 for (x in 1 .. 5 ) send(x * x)
10711056}
10721057
@@ -1084,22 +1069,22 @@ fun main(args: Array<String>) = runBlocking<Unit> {
10841069Pipeline is a pattern where one coroutine is producing, possibly infinite, stream of values:
10851070
10861071``` kotlin
1087- fun produceNumbers () = buildChannel <Int >(CommonPool ) {
1072+ fun produceNumbers () = produce <Int >(CommonPool ) {
10881073 var x = 1
10891074 while (true ) send(x++ ) // infinite stream of integers starting from 1
10901075}
10911076```
10921077
1093- And another coroutine or coroutines are receiving that stream, doing some processing, and sending the result .
1078+ And another coroutine or coroutines are consuming that stream, doing some processing, and producing some other results .
10941079In the below example the numbers are just squared:
10951080
10961081``` kotlin
1097- fun square (numbers : ReceiveChannel <Int >) = buildChannel <Int >(CommonPool ) {
1082+ fun square (numbers : ReceiveChannel <Int >) = produce <Int >(CommonPool ) {
10981083 for (x in numbers) send(x * x)
10991084}
11001085```
11011086
1102- The main code starts and connects pipeline:
1087+ The main code starts and connects the whole pipeline:
11031088
11041089``` kotlin
11051090fun main (args : Array <String >) = runBlocking<Unit > {
@@ -1131,7 +1116,7 @@ import kotlin.coroutines.experimental.CoroutineContext
11311116-->
11321117
11331118``` kotlin
1134- fun numbersFrom (context : CoroutineContext , start : Int ) = buildChannel <Int >(context) {
1119+ fun numbersFrom (context : CoroutineContext , start : Int ) = produce <Int >(context) {
11351120 var x = start
11361121 while (true ) send(x++ ) // infinite stream of integers from start
11371122}
@@ -1141,7 +1126,7 @@ The following pipeline stage filters an incoming stream of numbers, removing all
11411126that are divisible by the given prime number:
11421127
11431128``` kotlin
1144- fun filter (context : CoroutineContext , numbers : ReceiveChannel <Int >, prime : Int ) = buildChannel <Int >(context) {
1129+ fun filter (context : CoroutineContext , numbers : ReceiveChannel <Int >, prime : Int ) = produce <Int >(context) {
11451130 for (x in numbers) if (x % prime != 0 ) send(x)
11461131}
11471132```
@@ -1150,7 +1135,7 @@ Now we build our pipeline by starting a stream of numbers from 2, taking a prime
11501135and launching new pipeline stage for each prime number found:
11511136
11521137```
1153- numbers -> filter(2) -> filter(3) -> filter(5) -> filter(7) ...
1138+ numbersFrom(2) -> filter(2) -> filter(3) -> filter(5) -> filter(7) ...
11541139```
11551140
11561141The following example prints the first ten prime numbers,
@@ -1184,16 +1169,16 @@ The output of this code is:
1184116929
11851170```
11861171
1187- Note, that you can build the same pipeline using ` buildIterator ` from the standard library.
1188- Replace ` buildSequence ` with ` buildIterator ` , ` send ` with ` yield ` , ` receive ` with ` next ` ,
1172+ Note, that you can build the same pipeline using ` buildIterator ` coroutine builder from the standard library.
1173+ Replace ` produce ` with ` buildIterator ` , ` send ` with ` yield ` , ` receive ` with ` next ` ,
11891174` ReceiveChannel ` with ` Iterator ` , and get rid of the context. You will not need ` runBlocking ` either.
11901175However, the benefit of a pipeline that uses channels as shown above is that it can actually use
11911176multiple CPU cores if you run it in [ CommonPool] context.
11921177
1193- Anyway, this is an extremely impractical way to find prime numbers. In practise , pipelines do involve some
1178+ Anyway, this is an extremely impractical way to find prime numbers. In practice , pipelines do involve some
11941179other suspending invocations (like asynchronous calls to remote services) and these pipelines cannot be
11951180built using ` buildSeqeunce ` /` buildIterator ` , because they do not allow arbitrary suspension, unlike
1196- ` buildChannel ` which is fully asynchronous.
1181+ ` produce ` which is fully asynchronous.
11971182
11981183### Fan-out
11991184
@@ -1202,7 +1187,7 @@ Let us start with a producer coroutine that is periodically producing integers
12021187(ten numbers per second):
12031188
12041189``` kotlin
1205- fun produceNumbers () = buildChannel <Int >(CommonPool ) {
1190+ fun produceNumbers () = produce <Int >(CommonPool ) {
12061191 var x = 1 // start from 1
12071192 while (true ) {
12081193 send(x++ ) // produce next
@@ -1303,7 +1288,7 @@ The channels shown so far had no buffer. Unbuffered channels transfer elements w
13031288meet each other (aka rendezvous). If send is invoked first, then it is suspended until receive is invoked,
13041289if receive is invoked first, it is suspended until send is invoked.
13051290
1306- Both [ Channel()] [ Channel.invoke ] factory function and [ buildChannel ] builder take an optional ` capacity ` parameter to
1291+ Both [ Channel()] [ Channel.invoke ] factory function and [ produce ] builder take an optional ` capacity ` parameter to
13071292specify _ buffer size_ . Buffer allows senders to send multiple elements before suspending,
13081293similar to the ` BlockingQueue ` with a specified capacity, which blocks when buffer is full.
13091294
@@ -1369,5 +1354,5 @@ The first four elements are added to the buffer and the sender suspends when try
13691354[ ReceiveChannel.receive ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-receive-channel/receive.html
13701355[ SendChannel.close ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-send-channel/close.html
13711356[ SendChannel.send ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-send-channel/send.html
1372- [ buildChannel ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/build-channel .html
1357+ [ produce ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/produce .html
13731358<!-- - END -->
0 commit comments