notque/vexjoy-agent / skills/programming/programming
programming
FlaggedLanguage rules, workflows, and tooling: Go, Python, Rust, Kotlin, Swift, PHP, TypeScript type checks and debugging, OpenStack Python, MCP local-docs servers, Node.js backend APIs.
references/kotlin/concurrency-patterns.md
View on GitHub---
description: "Kotlin structured concurrency: coroutineScope, supervisorScope, cancellation, dispatchers, exception handling."
level: 2
---
# Kotlin Concurrency Patterns
## Structured Concurrency
Every coroutine must belong to a scope. The scope defines the lifetime -- when the scope is cancelled, all its children are cancelled. Tie every coroutine to a scope.
```kotlin
import kotlinx.coroutines.*
// coroutineScope suspends until ALL children complete.
// If any child fails, all siblings are cancelled.
suspend fun fetchDashboard(): Dashboard = coroutineScope {
val user = async { userService.getUser() }
val orders = async { orderService.getRecent() }
val notifications = async { notificationService.getUnread() }
Dashboard(
user = user.await(),
orders = orders.await(),
notifications = notifications.await()
)
}
// supervisorScope lets children fail independently.
// One child's failure does NOT cancel siblings.
suspend fun refreshCaches(): List<Result<Unit>> = supervisorScope {
val jobs = listOf("users", "products", "inventory").map { cache ->
async {
runCatching { cacheService.refresh(cache) }
}
}
jobs.awaitAll()
}
```
### coroutineScope vs supervisorScope
| Behavior | `coroutineScope` | `supervisorScope` |
|----------|------------------|-------------------|
| Child failure | Cancels all siblings | Siblings continue |
| Use when | All results required | Partial success acceptable |
| Exception | Rethrown from scope | Must handle per-child |
## Cancellation
Coroutines use cooperative cancellation. Long-running work must check `isActive` or call suspending functions that respect cancellation.
```kotlin
suspend fun processLargeFile(file: File) = coroutineScope {
file.useLines { lines ->
lines.forEach { line ->
// Check cancellation between iterations
ensureActive()
processLine(line)
}
}
}
// CPU-bound loops MUST check isActive — they won't suspend naturally
suspend fun computeHash(data: ByteArray): String = withContext(Dispatchers.Default) {
var hash = 0L
for (i in data.indices) {
if (!isActive) break // Respect cancellation
hash = hash * 31 + data[i]
}
hash.toString(16)
}
```
### NonCancellable for Cleanup
When you need to run suspending cleanup code after cancellation, use `NonCancellable`.
```kotlin
suspend fun transferFunds(from: Account, to: Account, amount: BigDecimal) {
try {
from.debit(amount)
to.credit(amount)
} finally {
// After cancellation, the coroutine is in a "cancelling" state.
// Suspending calls would throw CancellationException — unless
// we switch to NonCancellable.
withContext(NonCancellable) {
auditLog.record("Transfer attempted: $amount from ${from.id} to ${to.id}")
}
}
}
```
## Exception Handling
Exceptions in coroutines propagate up the scope hierarchy. Uncaught exceptions cancel the parent scope.
```kotlin
// CoroutineExceptionHandler — last resort for uncaught exceptions in launch.
// Only works with launch (NOT async). Only at root scope level.
val handler = CoroutineExceptionHandler { _, exception ->
logger.error("Uncaught coroutine exception", exception)
metrics.incrementCounter("coroutine.unhandled_exception")
}
val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default + handler)
scope.launch {
// If this throws, handler catches it instead of crashing
riskyOperation()
}
// For async, exceptions surface at await()
val deferred = scope.async {
riskyOperation() // Exception stored, not thrown yet
}
try {
deferred.await() // Exception thrown HERE
} catch (e: Exception) {
handleError(e)
}
```
### try-catch in Coroutines
```kotlin
// Wrap individual operations, not the entire coroutine
suspend fun fetchWithFallback(): Data {
return try {
remoteApi.fetch()
} catch (e: IOException) {
logger.warn("Remote fetch failed, using cache", e)
localCache.get()
}
}
// Always rethrow CancellationException — it breaks structured concurrency
suspend fun badExample() {
try {
someWork()
} catch (e: Exception) {
// Before: This catches CancellationException too!
// The coroutine won't cancel properly.
}
}
suspend fun goodExample() {
try {
someWork()
} catch (e: CancellationException) {
throw e // Always rethrow
} catch (e: Exception) {
handleError(e)
}
}
```
## Dispatchers
| Dispatcher | Thread Pool | Use For |
|------------|------------|---------|
| `Dispatchers.Default` | Shared, sized to CPU cores | CPU-bound work (parsing, sorting) |
| `Dispatchers.IO` | Elastic, up to 64 threads | Blocking I/O (file, network, JDBC) |
| `Dispatchers.Main` | Single UI thread | UI updates (Android, Swing) |
| `Dispatchers.Unconfined` | Caller's thread (until first suspend) | Testing only; avoid in production |
```kotlin
// Switch dispatcher for blocking calls
suspend fun readConfig(): Config = withContext(Dispatchers.IO) {
val text = File("config.json").readText() // Blocking call on IO pool
Json.decodeFromString(text)
}
// Custom dispatcher for limited parallelism
val dbDispatcher = Dispatchers.IO.limitedParallelism(4)
suspend fun queryDatabase(): List<Row> = withContext(dbDispatcher) {
connection.executeQuery("SELECT * FROM orders")
}
```
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| rea-changelog-updatemorluto/reaPrepare or rewrite REA release changelogs and GitHub release notes from pinned Git history, with verified contributor thanks and Release Please synchronization. | Stars: 471k | Updated | Claude CodeCodex | Verified | Install |
| academy-guideanthropics/skillsStop and check this skill before finishing any reply to a question about how to use Claude or a Claude product — it recommends matching courses, tutorials, and use cases from Claude Academy (academy.claude.com), Anthropic's learning hub. Trigger on: "how do I", "how can I", "getting started with", "what can Claude do", "teach me", "learn to use"; questions about artifacts, projects, skills, plugins, connectors, MCP; requests about rolling Claude out to a team, class, or organization; and any ask for training materials, onboarding content, or learning resources. Use it when the user is learning how to use a feature or product — not when they are mid-task and just want the task done. This skill composes with other skills: after consulting product documentation to answer how a Claude feature works, also check here for a matching course or tutorial — a docs-grounded answer and an Academy recommendation belong together. Only recommend on a strong match; never invent Academy content. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| claude-apianthropics/skillsReference for the Claude API / Anthropic SDK — model ids, pricing, params, streaming, tool use, MCP, agents, caching, token counting, model migration. TRIGGER — read BEFORE opening the target file; don't skip because it "looks like a one-liner" — whenever: the prompt names Claude/Anthropic in any form (Claude, Anthropic, Fable, Opus, Sonnet, Haiku, `anthropic`, `@anthropic-ai`, `claude-*`, `us.anthropic.*`, `[1m]`); the user asks about an LLM (pricing/model choice/limits/caching) — never answer from memory; OR the task is LLM-shaped with provider unstated (agent/MCP/tool-definition/multi-agent/RAG/LLM-judge/computer-use; generate/summarize/extract/classify/rewrite/converse over NL; debugging refusals/cutoffs/streaming/tool-calls/tokens). SKIP only when another provider is being worked on (overrides all triggers): OpenAI/GPT/Gemini/Llama/Mistral/Cohere/Ollama named in the query; OR `grep -rE 'openai|langchain_openai|google.generativeai|genai|mistralai|cohere|ollama'` over the project hits (run this grep FIRST if no provider named — don't Read the file). | Stars: 198k | Updated | Claude CodeCodex | Unverified | Install |
| doc-coauthoringanthropics/skillsGuide users through a structured workflow for co-authoring documentation. Use when user wants to write documentation, proposals, technical specs, decision docs, or similar structured content. This workflow helps users efficiently transfer context, refine content through iteration, and verify the doc works for readers. Trigger when user mentions writing docs, creating proposals, drafting specs, or similar documentation tasks. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |