Skip to content
TopicTracker
From HackerNewsView original
TranslationTranslation

Rewriting 3 files to switch 1M LoC from Java threads to Kotlin coroutines

The article describes how a team switched a codebase of over a million lines from Java threads to Kotlin coroutines by rewriting only three core files: a custom dispatcher, a runnable wrapper, and a utility function. This approach minimized changes while enabling coroutine-based concurrency across the entire project.

Background

Kotlin coroutines are a modern concurrency framework for the JVM / Android that lets developers write non-blocking, asynchronous code in a sequential style (using `suspend` functions), avoiding the complexity of manually managing Java threads. - **Java threads**: The traditional way to run code in parallel on Android/JVM. Creating a thread per background task is simple but expensive in memory and CPU, and manual thread-pool management is error-prone. - **Kotlin coroutines**: Lightweight concurrency primitives that run on a shared thread pool, can be paused (suspended) without blocking a thread, and automatically cancel when a scope ends — far less boilerplate and fewer bugs related to leaks or race conditions. - **Why this matters**: Many Android apps were written in Java with hand-rolled threading. Migrating to coroutines can improve stability and performance, but rewriting a million lines of code project-by-project is daunting. This article claims that a well-designed "threading layer" abstraction lets you swap the underlying mechanism (threads → coroutines) by changing just three files — a strategy relevant to large, long-lived codebases.