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

Is This a Monad?

References Monad English Wiki: https://en.wikipedia.org/wiki/Monad_(functional_programming) Function English Wiki: https://en.wikipedia.org/wiki/Function_(mathematics) Function Korean Wiki: https://ko.wikipedia.org/wiki/함수 Introduction This time, I want to take a different approach. Instead of explaining what a monad is right away, let’s consider the problems encountered when composing functions, and then present solutions to understand what can be called a monad. So, let’s first revisit what a function is, to improve understanding, and then explain the problematic example. Please note that this post contains personal opinions. As the title suggests, I can’t confidently say “this is a monad!” ...

June 25, 2023

Stream API - peek

peek? Why does the documentation recommend using Stream.peek mainly for debugging? Let’s find out. Reference Stream API Since peek is part of the Stream API, let’s start by looking at the Stream API documentation. According to the Stream API documentation, functions passed to methods like filter and map must satisfy two constraints for correct operation: They must be non-interfering In most cases, they should be stateless Let’s focus on non-interfering. non-interfering Dictionary meaning of interfering Interfering, meddlesome ...

December 13, 2022

Lambda Expression and Method Reference Differences

Users Sort by last name If the last names are the same, sort by first name Then return as a List. I created an example for cases where there are two sorting criteria. And I tried to solve the problem using method reference and lambda expression… but what happened… The example written with a lambda expression results in a compile error. Huh! Is it not able to infer the type well…? They look exactly the same, though. ...

October 13, 2022

Stream API Practice

Recently, I read the book Modern Java In Action. I was very impressed. I realized that when studying functional programming and the Java API, I should read books. Compared to Googling, books are much richer in content and much more reliable. After all, it was written by a Java Champion, two professors, and one engineer! Anyway, I will leave some code I wrote while studying the stream API. And I will gradually share the ideas behind why I wrote the code this way. ...

October 13, 2022

Collections Summary - Interfaces

Related Posts https://jurogrammer.tistory.com/172 Interfaces https://docs.oracle.com/javase/tutorial/collections/interfaces/index.html Overview Core collection interfaces encapsulate different types of collections. Therefore, you can manipulate different collections without worrying about the details. The collection interface can be considered the foundation of the Java Collection Framework. The Interface chapter will provide general guidelines for efficient use of collection interfaces. Important Notes Map is not a Collection Literally, it’s not. All Core collections are Generic When declaring a collection, you must specify the type that will go into the collection. Generics can verify at compile time whether the object being put into the collection is of the correct type. Java Platform does not provide interface variations To facilitate the management of numerous core collection interfaces, the Java Platform does not provide interface variations (e.g., interfaces with characteristics like immutable, fixed-size, append-only). Instead, modification operations are optional. That is, since not all operations are implemented, calling a specific operation may throw an UnsupportedOperationException. Therefore, this must be documented. In summary, if you need to implement a specific collection interface, implement only what you need. Don’t implement everything. ...

June 30, 2022

Closure

Introduction Let’s look up the word ‘closure’ in English. Dictionary: an act or process of closing something, especially an institution, thoroughfare, or frontier, or of being closed. It means ’to close’. So, when I first studied closures, it didn’t make sense to me. What is being closed? Today, let’s lift the veil! The Concept of Closure Lambda calculus is a concept from the 1930s, and closure is from the 1960s. ...

August 8, 2021

Lazy Evaluation

Introduction The concept of lazy evaluation is not difficult. So, this time, I will focus on its usefulness. Background Knowledge If you are confused between expression and evaluation, please refer to the following article: https://jurogrammer.tistory.com/129 Definition Lazy evaluation is a strategy where the evaluation of an expression is delayed until its value is needed. It is usually used to improve performance. Let’s see how delaying evaluation can improve performance. Example of Performance Improvement Here is an example where a value is only used if a certain condition is met. ...

August 8, 2021