Skip to content

Commit cbeef10

Browse files
AlexanderPrendotaqwwdfsad
authored andcommitted
fix broken links for kotlin web site (#628)
* fix broken links for Kotlin web site
1 parent b590aa3 commit cbeef10

8 files changed

+365
-6
lines changed

docs/basics.md

+44
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ This section covers basic coroutine concepts.
4343

4444
Run the following code:
4545

46+
<div class="sample" markdown="1" theme="idea" data-highlight-only>
47+
4648
```kotlin
4749
fun main(args: Array<String>) {
4850
GlobalScope.launch { // launch new coroutine in background and continue
@@ -54,6 +56,8 @@ fun main(args: Array<String>) {
5456
}
5557
```
5658

59+
</div>
60+
5761
> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-basic-01.kt)
5862
5963
Run this code:
@@ -88,6 +92,8 @@ The first example mixes _non-blocking_ `delay(...)` and _blocking_ `Thread.sleep
8892
It is easy to get lost which one is blocking and which one is not.
8993
Let's be explicit about blocking using [runBlocking] coroutine builder:
9094

95+
<div class="sample" markdown="1" theme="idea" data-highlight-only>
96+
9197
```kotlin
9298
fun main(args: Array<String>) {
9399
GlobalScope.launch { // launch new coroutine in background and continue
@@ -101,6 +107,8 @@ fun main(args: Array<String>) {
101107
}
102108
```
103109

110+
</div>
111+
104112
> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-basic-02.kt)
105113
106114
<!--- TEST
@@ -114,6 +122,8 @@ The main thread, that invokes `runBlocking`, _blocks_ until the coroutine inside
114122
This example can be also rewritten in a more idiomatic way, using `runBlocking` to wrap
115123
the execution of the main function:
116124

125+
<div class="sample" markdown="1" theme="idea" data-highlight-only>
126+
117127
```kotlin
118128
fun main(args: Array<String>) = runBlocking { // start main coroutine
119129
GlobalScope.launch { // launch new coroutine in background and continue
@@ -125,6 +135,8 @@ fun main(args: Array<String>) = runBlocking { // start main coroutine
125135
}
126136
```
127137

138+
</div>
139+
128140
> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-basic-02b.kt)
129141
130142
<!--- TEST
@@ -136,6 +148,8 @@ Here `runBlocking<Unit> { ... }` works as an adaptor that is used to start the t
136148
We explicitly specify its `Unit` return type, because a well-formed `main` function in Kotlin has to return `Unit`.
137149

138150
This is also a way to write unit-tests for suspending functions:
151+
152+
<div class="sample" markdown="1" theme="idea" data-highlight-only>
139153

140154
```kotlin
141155
class MyTest {
@@ -146,13 +160,17 @@ class MyTest {
146160
}
147161
```
148162

163+
</div>
164+
149165
<!--- CLEAR -->
150166

151167
### Waiting for a job
152168

153169
Delaying for a time while another coroutine is working is not a good approach. Let's explicitly
154170
wait (in a non-blocking way) until the background [Job] that we have launched is complete:
155171

172+
<div class="sample" markdown="1" theme="idea" data-highlight-only>
173+
156174
```kotlin
157175
fun main(args: Array<String>) = runBlocking {
158176
val job = GlobalScope.launch { // launch new coroutine and keep a reference to its Job
@@ -164,6 +182,8 @@ fun main(args: Array<String>) = runBlocking {
164182
}
165183
```
166184

185+
</div>
186+
167187
> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-basic-03.kt)
168188
169189
<!--- TEST
@@ -193,6 +213,8 @@ We can launch coroutines in this scope without having to `join` them explicitly,
193213
an outer coroutine (`runBlocking` in our example) does not complete until all the coroutines launched
194214
in its scope complete. Thus, we can make our example simpler:
195215

216+
<div class="sample" markdown="1" theme="idea" data-highlight-only>
217+
196218
```kotlin
197219
fun main(args: Array<String>) = runBlocking { // this: CoroutineScope
198220
launch { // launch new coroutine in the scope of runBlocking
@@ -203,6 +225,8 @@ fun main(args: Array<String>) = runBlocking { // this: CoroutineScope
203225
}
204226
```
205227

228+
</div>
229+
206230
> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-basic-03s.kt)
207231
208232
<!--- TEST
@@ -216,6 +240,8 @@ In addition to the coroutine scope provided by different builders, it is possibl
216240
complete. The main difference between [runBlocking] and [coroutineScope] is that the latter does not block the current thread
217241
while waiting for all children to complete.
218242

243+
<div class="sample" markdown="1" theme="idea" data-highlight-only>
244+
219245
```kotlin
220246
fun main(args: Array<String>) = runBlocking { // this: CoroutineScope
221247
launch {
@@ -237,6 +263,8 @@ fun main(args: Array<String>) = runBlocking { // this: CoroutineScope
237263
}
238264
```
239265

266+
</div>
267+
240268
> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-basic-04.kt)
241269
242270
<!--- TEST
@@ -254,6 +282,8 @@ That is your first _suspending function_. Suspending functions can be used insid
254282
just like regular functions, but their additional feature is that they can, in turn,
255283
use other suspending functions, like `delay` in this example, to _suspend_ execution of a coroutine.
256284

285+
<div class="sample" markdown="1" theme="idea" data-highlight-only>
286+
257287
```kotlin
258288
fun main(args: Array<String>) = runBlocking {
259289
launch { doWorld() }
@@ -267,6 +297,8 @@ suspend fun doWorld() {
267297
}
268298
```
269299

300+
</div>
301+
270302
> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-basic-05.kt)
271303
272304
<!--- TEST
@@ -280,6 +312,8 @@ In this case `suspend` modifier on the extracted function is not enough. Making
280312
method on `CoroutineScope` is one of the solutions, but it may not always be applicable as it does not make API clearer.
281313
[currentScope] builder comes to help: it inherits current [CoroutineScope] from the coroutine context it is invoked.
282314

315+
<div class="sample" markdown="1" theme="idea" data-highlight-only>
316+
283317
```kotlin
284318
fun main(args: Array<String>) = runBlocking {
285319
launchDoWorld()
@@ -294,6 +328,8 @@ suspend fun launchDoWorld() = currentScope {
294328
}
295329
```
296330

331+
</div>
332+
297333
> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-basic-05s.kt)
298334
299335
<!--- TEST
@@ -305,6 +341,8 @@ World!
305341

306342
Run the following code:
307343

344+
<div class="sample" markdown="1" theme="idea" data-highlight-only>
345+
308346
```kotlin
309347
fun main(args: Array<String>) = runBlocking {
310348
repeat(100_000) { // launch a lot of coroutines
@@ -316,6 +354,8 @@ fun main(args: Array<String>) = runBlocking {
316354
}
317355
```
318356

357+
</div>
358+
319359
> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-basic-06.kt)
320360
321361
<!--- TEST lines.size == 1 && lines[0] == ".".repeat(100_000) -->
@@ -328,6 +368,8 @@ Now, try that with threads. What would happen? (Most likely your code will produ
328368
The following code launches a long-running coroutine in [GlobalScope] that prints "I'm sleeping" twice a second and then
329369
returns from the main function after some delay:
330370

371+
<div class="sample" markdown="1" theme="idea" data-highlight-only>
372+
331373
```kotlin
332374
fun main(args: Array<String>) = runBlocking {
333375
GlobalScope.launch {
@@ -340,6 +382,8 @@ fun main(args: Array<String>) = runBlocking {
340382
}
341383
```
342384

385+
</div>
386+
343387
> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-basic-07.kt)
344388
345389
You can run and see that it prints three lines and terminates:

docs/cancellation-and-timeouts.md

+29
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ For example, a user might have closed the page that launched a coroutine and now
4242
is no longer needed and its operation can be cancelled.
4343
The [launch] function returns a [Job] that can be used to cancel running coroutine:
4444

45+
<div class="sample" markdown="1" theme="idea" data-highlight-only>
46+
4547
```kotlin
4648
fun main(args: Array<String>) = runBlocking {
4749
val job = launch {
@@ -58,6 +60,8 @@ fun main(args: Array<String>) = runBlocking {
5860
}
5961
```
6062

63+
</div>
64+
6165
> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-cancel-01.kt)
6266
6367
It produces the following output:
@@ -84,6 +88,8 @@ coroutine and throw [CancellationException] when cancelled. However, if a corout
8488
a computation and does not check for cancellation, then it cannot be cancelled, like the following
8589
example shows:
8690

91+
<div class="sample" markdown="1" theme="idea" data-highlight-only>
92+
8793
```kotlin
8894
fun main(args: Array<String>) = runBlocking {
8995
val startTime = System.currentTimeMillis()
@@ -105,6 +111,8 @@ fun main(args: Array<String>) = runBlocking {
105111
}
106112
```
107113

114+
</div>
115+
108116
> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-cancel-02.kt)
109117
110118
Run it to see that it continues to print "I'm sleeping" even after cancellation
@@ -128,6 +136,8 @@ The other one is to explicitly check the cancellation status. Let us try the lat
128136

129137
Replace `while (i < 5)` in the previous example with `while (isActive)` and rerun it.
130138

139+
<div class="sample" markdown="1" theme="idea" data-highlight-only>
140+
131141
```kotlin
132142
fun main(args: Array<String>) = runBlocking {
133143
val startTime = System.currentTimeMillis()
@@ -149,6 +159,8 @@ fun main(args: Array<String>) = runBlocking {
149159
}
150160
```
151161

162+
</div>
163+
152164
> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-cancel-03.kt)
153165
154166
As you can see, now this loop is cancelled. [isActive] is an extension property that is
@@ -168,6 +180,9 @@ Cancellable suspending functions throw [CancellationException] on cancellation w
168180
a usual way. For example, `try {...} finally {...}` expression and Kotlin `use` function execute their
169181
finalization actions normally when coroutine is cancelled:
170182

183+
184+
<div class="sample" markdown="1" theme="idea" data-highlight-only>
185+
171186
```kotlin
172187
fun main(args: Array<String>) = runBlocking {
173188
val job = launch {
@@ -187,6 +202,8 @@ fun main(args: Array<String>) = runBlocking {
187202
}
188203
```
189204

205+
</div>
206+
190207
> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-cancel-04.kt)
191208
192209
Both [join][Job.join] and [cancelAndJoin] wait for all the finalization actions to complete,
@@ -212,6 +229,8 @@ communication channel) are usually non-blocking and do not involve any suspendin
212229
rare case when you need to suspend in the cancelled coroutine you can wrap the corresponding code in
213230
`withContext(NonCancellable) {...}` using [withContext] function and [NonCancellable] context as the following example shows:
214231

232+
<div class="sample" markdown="1" theme="idea" data-highlight-only>
233+
215234
```kotlin
216235
fun main(args: Array<String>) = runBlocking {
217236
val job = launch {
@@ -235,6 +254,8 @@ fun main(args: Array<String>) = runBlocking {
235254
}
236255
```
237256

257+
</div>
258+
238259
> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-cancel-05.kt)
239260
240261
<!--- TEST
@@ -255,6 +276,8 @@ While you can manually track the reference to the corresponding [Job] and launch
255276
the tracked one after delay, there is a ready to use [withTimeout] function that does it.
256277
Look at the following example:
257278

279+
<div class="sample" markdown="1" theme="idea" data-highlight-only>
280+
258281
```kotlin
259282
fun main(args: Array<String>) = runBlocking {
260283
withTimeout(1300L) {
@@ -266,6 +289,8 @@ fun main(args: Array<String>) = runBlocking {
266289
}
267290
```
268291

292+
</div>
293+
269294
> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-cancel-06.kt)
270295
271296
It produces the following output:
@@ -289,6 +314,8 @@ You can wrap the code with timeout in `try {...} catch (e: TimeoutCancellationEx
289314
you need to do some additional action specifically on any kind of timeout or use [withTimeoutOrNull] function
290315
that is similar to [withTimeout], but returns `null` on timeout instead of throwing an exception:
291316

317+
<div class="sample" markdown="1" theme="idea" data-highlight-only>
318+
292319
```kotlin
293320
fun main(args: Array<String>) = runBlocking {
294321
val result = withTimeoutOrNull(1300L) {
@@ -302,6 +329,8 @@ fun main(args: Array<String>) = runBlocking {
302329
}
303330
```
304331

332+
</div>
333+
305334
> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-cancel-07.kt)
306335
307336
There is no longer an exception when running this code:

0 commit comments

Comments
 (0)