-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mention swallowing CancellationExceptions in the docs (#3302)
- Loading branch information
1 parent
937f430
commit 9ea4fea
Showing
10 changed files
with
137 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
kotlinx-coroutines-core/jvm/test/guide/example-cancel-10.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
// This file was automatically generated from cancellation-and-timeouts.md by Knit tool. Do not edit. | ||
package kotlinx.coroutines.guide.exampleCancel10 | ||
|
||
import kotlinx.coroutines.* | ||
|
||
var acquired = 0 | ||
|
||
class Resource { | ||
init { acquired++ } // Acquire the resource | ||
fun close() { acquired-- } // Release the resource | ||
} | ||
|
||
fun main() { | ||
runBlocking { | ||
repeat(100_000) { // Launch 100K coroutines | ||
launch { | ||
var resource: Resource? = null // Not acquired yet | ||
try { | ||
withTimeout(60) { // Timeout of 60 ms | ||
delay(50) // Delay for 50 ms | ||
resource = Resource() // Store a resource to the variable if acquired | ||
} | ||
// We can do something else with the resource here | ||
} finally { | ||
resource?.close() // Release the resource if it was acquired | ||
} | ||
} | ||
} | ||
} | ||
// Outside of runBlocking all coroutines have completed | ||
println(acquired) // Print the number of resources still acquired | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters