Structured Concurrency

Source jep480 (Java Enhancement Proposal) Original post introducing structured concurrency (2016) Summary of the jep480 document. Purpose of This Post Introduce a concurrency programming style that can eliminate common risks from cancellation and shutdown. Improve the observability of concurrent code. Motivation Developers manage complexity by dividing tasks into subtasks. In single-threaded code, subtasks are executed sequentially, but if subtasks are independent, running them concurrently can improve performance. However, managing many threads is very difficult. Unstructured concurrency with ExecutorService Example code using ExecutorService introduced in Java 5 for concurrency, showing what problems arise without structured concurrency. ...

June 2, 2025

When Constructor Use Cases

Content Summary of ‘when’ usage cases Source - Kotlin in Action What is ‘when’? A more powerful version of Java’s switch. Java switch Improves readability of if-else branching by using switch. However, branch arguments are limited to constants (mainly enums, strings). Kotlin when Branches can use not only constants (enums, strings) but also arbitrary objects and expressions—no restrictions. A universal branching statement. Use Cases 1. Branching on enums to return a specific string enum class 1 2 3 enum class Color { RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET } when code 1 2 3 4 5 6 7 8 9 10 fun getMnemonic(color: Color) = when (color) { Color.RED -> "Richard" Color.ORANGE -> "Of" Color.YELLOW -> "York" Color.GREEN -> "Gave" Color.BLUE -> "Battle" Color.INDIGO -> "In" Color.VIOLET -> "Vain" } No need for break; can use arrows With arrow functions in Java switch, this is now similar in Java Up to here, you might think “Isn’t this just like Java?” ...

June 2, 2025

Why CancellationException Should Be Rethrown

Reference It’s easier to understand if you think of a thread’s interrupt as a job’s cancel. When using runCatching, you must write code to rethrow the CancellationException. Kotlin does not provide a convenience method for this. - Related Issue Reason When a job is cancelled, it should not continue running. However, if you catch a CancellationException, the job may continue to run, so it is recommended to rethrow it. Problematic Code If you do not rethrow, a job that is already cancelled may continue to run. ...

June 2, 2025

Spring AOP Self-Invocation Problem and a Kotlinic Workaround

Spring AOP Problem In Spring, self-invocation refers to a method within a class calling another method of the same class. This can be problematic when using AOP (Aspect-Oriented Programming) annotations like @Transactional. Spring AOP is proxy-based. So if you call another method directly within the same class, it bypasses the proxy, and AOP annotations won’t be applied. For example, suppose you have method A annotated with @Transactional, and it’s called from method B in the same class. If method B calls A directly, Spring won’t manage the transaction properly because the call doesn’t go through the proxy. ...

May 27, 2024

Fixture Monkey Example

Fixture Monkey Example Fixture Monkey - https://naver.github.io/fixture-monkey/v1-0-0/ For readability, it is recommended to copy and paste the code into your IDE. 1 // ... (all code blocks and explanations translated to English, preserving structure) (Translate all explanations, comments, and section headings to English. Keep all code blocks as-is. If you want the full translation, let me know and I will provide the complete English markdown.)