Skip to content

[MINOR][CORE] Remove redundant synchronized in ThreadUtils#50210

Closed
jinkachy wants to merge 1 commit intoapache:masterfrom
jinkachy:threadutil
Closed

[MINOR][CORE] Remove redundant synchronized in ThreadUtils#50210
jinkachy wants to merge 1 commit intoapache:masterfrom
jinkachy:threadutil

Conversation

@jinkachy
Copy link

@jinkachy jinkachy commented Mar 7, 2025

What changes were proposed in this pull request?

This PR removes the redundant synchronized keyword from the isTerminated method in sameThreadExecutorService() implementation within the ThreadUtils class. The method was using both a synchronized block and an explicit ReentrantLock, which is unnecessary and potentially problematic.

Why are the changes needed?

The changes are needed for several reasons:

  1. Eliminates redundant synchronization: The current implementation uses both synchronized keyword and explicit ReentrantLock, which is redundant and creates unnecessary overhead.

  2. Reduces potential deadlock risks: Using two different locking mechanisms (built-in synchronized and explicit ReentrantLock) in the same method could lead to complex lock ordering issues and increase deadlock risks.
    Although the risk of deadlock in the current implementation is low, if someone modifies the code in the future and adds a method that acquires these two locks in a different order, it would introduce a deadlock risk.

  3. Improves performance: Removing the unnecessary synchronization layer reduces lock contention and context switching overhead.

  4. Code consistency: Other methods in the same class only use ReentrantLock for synchronization, so removing synchronized makes the code style more consistent.

  5. More precise control: ReentrantLock already provides all the synchronization needed with more features than the implicit synchronized keyword.

Does this PR introduce any user-facing change?

No. This change is an internal implementation improvement that doesn't affect any public APIs or user-visible behavior.

How was this patch tested?

The existing tests for ThreadUtils were run to ensure that the behavior of sameThreadExecutorService() remains unchanged. No additional tests were added as this is a straightforward removal of redundant synchronization that doesn't alter the method's behavior or contract.

Was this patch authored or co-authored using generative AI tooling?

No.

@github-actions github-actions bot added the CORE label Mar 7, 2025
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @srowen It seems is right.

@beliefer
Copy link
Contributor

beliefer commented Mar 7, 2025

Please config github action.

@beliefer beliefer changed the title [MINOR][SPARK-CORE] Remove redundant synchronized in ThreadUtils [MINOR][CORE] Remove redundant synchronized in ThreadUtils Mar 7, 2025
Copy link
Member

@srowen srowen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, looks left over from some other previous version. This is the only synchronized method which won't do anything

Copy link
Member

@dongjoon-hyun dongjoon-hyun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, LGTM.

@jinkachy
Copy link
Author

jinkachy commented Mar 7, 2025

Please config github action.

done

@dongjoon-hyun
Copy link
Member

dongjoon-hyun commented Mar 7, 2025

Merged to master for Apache Spark 4.1.0. Thank you, @jinkachy and all.

SteNicholas added a commit to apache/celeborn that referenced this pull request Mar 13, 2025
…adUtils#sameThreadExecutorService

### What changes were proposed in this pull request?

Remove the redundant synchronized keyword from the `isTerminated` method in `sameThreadExecutorService()` implementation within the `ThreadUtils` class. The method was using both a synchronized block and an explicit `ReentrantLock`, which is unnecessary and potentially problematic.

Backport apache/spark#50210.

### Why are the changes needed?

The changes are needed for several reasons:

1. **Eliminates redundant synchronization**: The current implementation uses both synchronized keyword and explicit ReentrantLock, which is redundant and creates unnecessary overhead.
2. **Reduces potential deadlock risks**: Using two different locking mechanisms (built-in synchronized and explicit `ReentrantLock`) in the same method could lead to complex lock ordering issues and increase deadlock risks. Although the risk of deadlock in the current implementation is low, if someone modifies the code in the future and adds a method that acquires these two locks in a different order, it would introduce a deadlock risk.
3. **Improves performance**: Removing the unnecessary synchronization layer reduces lock contention and context switching overhead.
4. **Code consistency**: Other methods in the same class only use `ReentrantLock` for synchronization, so removing synchronized makes the code style more consistent.
5. **More precise control**: `ReentrantLock` already provides all the synchronization needed with more features than the implicit synchronized keyword.

### Does this PR introduce _any_ user-facing change?

No.

### How was this patch tested?

CI.

Closes #3150 from SteNicholas/CELEBORN-1910.

Authored-by: SteNicholas <programgeek@163.com>
Signed-off-by: SteNicholas <programgeek@163.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants