Repository navigation
Why does TaskletStep use a Semaphore? #5459
Replies: 1 comment
|
History points to this being a binary lock, rather than a deliberate use of cross-thread permit handoff. Before 2008, BATCH-1708 later moved the semaphore from an instance field into The important current behavior is the lock lifetime: it is acquired immediately before applying the contribution and updating the shared A |
Uh oh!
There was an error while loading. Please reload this page.
Hi,
I’m currently studying the internals of Spring Batch, and I came across the use of a
SemaphoreinTaskletStep.Here is the relevant source code:
https://github.com/spring-projects/spring-batch/blob/main/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/TaskletStep.java
As I understand it, synchronization is needed because multiple chunks can run concurrently in a multi-threaded step, while their contributions are eventually applied to the same
StepExecutionand persisted through theJobRepository.What I am curious about is why
Semaphore(1)was chosen specifically.Since this seems to be a mutual-exclusion requirement, I initially thought that a lock such as
ReentrantLockcould also be used.Unlike an ownership-based lock, a semaphore allows a permit to be released by a thread other than the one that acquired it. Was this property needed in any intended execution scenario, or was the semaphore simply being used as a binary lock here?
I am not suggesting that the implementation should be changed. I am just interested in understanding whether there was a specific design or historical reason behind this choice.
I looked through the source code and some related issues, but I could only find explanations for why synchronization was needed, not why
Semaphoreitself was selected.Thanks in advance for any context.
All reactions