AI Briefing
KO

Improving Code Quality - Session 61: This Function Is Only for One Caller

·2026.01.09 11:00

Key point

This analyzes the concurrency issues that occur when function state is managed as an object's property, and presents two design approaches to resolve them.

Details

Using a Boolean property such as isCalculating inside a class to represent a function's execution state can be flawed in concurrent environments. When multiple calls execute simultaneously, if one call finishes and changes the state to false, the state ends up being displayed incorrectly even though other calls are still in progress.

This problem occurs because the state of a function call is treated as the state of the Receiver. In other words, when multiple calls share a single receiver, the individual state of each call cannot be distinguished. Two main design approaches to resolve this are as follows.

  • Include call state in the return value: Return an object that has independent state per call, such as CompletableFuture or Deferred, forming a 1:1 relationship between the call and its state.
  • Separate the receiver's state: Instead of simply using a Boolean, use something like an AtomicInteger that tracks the number of calls currently in progress, and check whether the call count is greater than 0.

This problem can occur not only in multi-threaded environments but also in single-threaded environments where recursive calls occur, so care is needed during design.

This summary was generated automatically by AI. Check the original for the author's claims and context. Copyright belongs to the original author.

Our guide explains how the AI works. Report summary errors, attribution issues, or removal requests via Contact.