Skip to content

Commit

Permalink
fix broken links for kotlin web site (#628)
Browse files Browse the repository at this point in the history
* fix broken links for Kotlin web site
  • Loading branch information
AlexanderPrendota authored and qwwdfsad committed Sep 27, 2018
1 parent b590aa3 commit cbeef10
Show file tree
Hide file tree
Showing 8 changed files with 365 additions and 6 deletions.
44 changes: 44 additions & 0 deletions docs/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ This section covers basic coroutine concepts.

Run the following code:

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
fun main(args: Array<String>) {
GlobalScope.launch { // launch new coroutine in background and continue
Expand All @@ -54,6 +56,8 @@ fun main(args: Array<String>) {
}
```

</div>

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

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
fun main(args: Array<String>) {
GlobalScope.launch { // launch new coroutine in background and continue
Expand All @@ -101,6 +107,8 @@ fun main(args: Array<String>) {
}
```

</div>

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

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
fun main(args: Array<String>) = runBlocking { // start main coroutine
GlobalScope.launch { // launch new coroutine in background and continue
Expand All @@ -125,6 +135,8 @@ fun main(args: Array<String>) = runBlocking { // start main coroutine
}
```

</div>

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

This is also a way to write unit-tests for suspending functions:

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
class MyTest {
Expand All @@ -146,13 +160,17 @@ class MyTest {
}
```

</div>

<!--- CLEAR -->

### Waiting for a job

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

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
fun main(args: Array<String>) = runBlocking {
val job = GlobalScope.launch { // launch new coroutine and keep a reference to its Job
Expand All @@ -164,6 +182,8 @@ fun main(args: Array<String>) = runBlocking {
}
```

</div>

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

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
fun main(args: Array<String>) = runBlocking { // this: CoroutineScope
launch { // launch new coroutine in the scope of runBlocking
Expand All @@ -203,6 +225,8 @@ fun main(args: Array<String>) = runBlocking { // this: CoroutineScope
}
```

</div>

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

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
fun main(args: Array<String>) = runBlocking { // this: CoroutineScope
launch {
Expand All @@ -237,6 +263,8 @@ fun main(args: Array<String>) = runBlocking { // this: CoroutineScope
}
```

</div>

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

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
fun main(args: Array<String>) = runBlocking {
launch { doWorld() }
Expand All @@ -267,6 +297,8 @@ suspend fun doWorld() {
}
```

</div>

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

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
fun main(args: Array<String>) = runBlocking {
launchDoWorld()
Expand All @@ -294,6 +328,8 @@ suspend fun launchDoWorld() = currentScope {
}
```

</div>

> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-basic-05s.kt)
<!--- TEST
Expand All @@ -305,6 +341,8 @@ World!

Run the following code:

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
fun main(args: Array<String>) = runBlocking {
repeat(100_000) { // launch a lot of coroutines
Expand All @@ -316,6 +354,8 @@ fun main(args: Array<String>) = runBlocking {
}
```

</div>

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

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
fun main(args: Array<String>) = runBlocking {
GlobalScope.launch {
Expand All @@ -340,6 +382,8 @@ fun main(args: Array<String>) = runBlocking {
}
```

</div>

> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-basic-07.kt)
You can run and see that it prints three lines and terminates:
Expand Down
29 changes: 29 additions & 0 deletions docs/cancellation-and-timeouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ For example, a user might have closed the page that launched a coroutine and now
is no longer needed and its operation can be cancelled.
The [launch] function returns a [Job] that can be used to cancel running coroutine:

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
fun main(args: Array<String>) = runBlocking {
val job = launch {
Expand All @@ -58,6 +60,8 @@ fun main(args: Array<String>) = runBlocking {
}
```

</div>

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

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
fun main(args: Array<String>) = runBlocking {
val startTime = System.currentTimeMillis()
Expand All @@ -105,6 +111,8 @@ fun main(args: Array<String>) = runBlocking {
}
```

</div>

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

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

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
fun main(args: Array<String>) = runBlocking {
val startTime = System.currentTimeMillis()
Expand All @@ -149,6 +159,8 @@ fun main(args: Array<String>) = runBlocking {
}
```

</div>

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


<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
fun main(args: Array<String>) = runBlocking {
val job = launch {
Expand All @@ -187,6 +202,8 @@ fun main(args: Array<String>) = runBlocking {
}
```

</div>

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

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
fun main(args: Array<String>) = runBlocking {
val job = launch {
Expand All @@ -235,6 +254,8 @@ fun main(args: Array<String>) = runBlocking {
}
```

</div>

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

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
fun main(args: Array<String>) = runBlocking {
withTimeout(1300L) {
Expand All @@ -266,6 +289,8 @@ fun main(args: Array<String>) = runBlocking {
}
```

</div>

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

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
fun main(args: Array<String>) = runBlocking {
val result = withTimeoutOrNull(1300L) {
Expand All @@ -302,6 +329,8 @@ fun main(args: Array<String>) = runBlocking {
}
```

</div>

> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-cancel-07.kt)
There is no longer an exception when running this code:
Expand Down
Loading

0 comments on commit cbeef10

Please sign in to comment.