fix: handle awaitTermination result and ensure proper ExecutorService shutdown#3244
fix: handle awaitTermination result and ensure proper ExecutorService shutdown#3244iluwatar merged 4 commits intoiluwatar:masterfrom
Conversation
… shutdown - Added handling for the result of awaitTermination to avoid Sonar warning - Wrapped ExecutorService with try-finally for proper shutdown (java:S2095) - Prevents potential resource leak and aligns with best practices Fixes: iluwatar#2865 Note: ExecutorService is not AutoCloseable, so try-with-resources is not applicable. Used try-finally instead.
PR SummaryThis PR addresses a high severity issue (java:S2095) reported by SonarCloud by improving resource handling and ensuring proper ExecutorService shutdown in the Changes
autogenerated by presubmit.ai |
There was a problem hiding this comment.
✅ LGTM!
Review Summary
Commits Considered (2)
-
c217d2d: Merge branch 'iluwatar:master' into master
-
634e77a: fix: handle awaitTermination result and ensure proper ExecutorService shutdown
-
Added handling for the result of awaitTermination to avoid Sonar warning
-
Wrapped ExecutorService with try-finally for proper shutdown (java:S2095)
-
Prevents potential resource leak and aligns with best practices
Fixes: #2865
Note: ExecutorService is not AutoCloseable, so try-with-resources is not applicable. Used try-finally instead.
Files Processed (1)
- leader-followers/src/main/java/com/iluwatar/leaderfollowers/App.java (1 hunk)
Actionable Comments (0)
Skipped Comments (1)
-
leader-followers/src/main/java/com/iluwatar/leaderfollowers/App.java [80-82]
possible issue: "Unnecessary
shutdownNow()call."
|
Hi @iluwatar, kindly reminding for review when you get a chance 🙂 |
| addTasks(taskSet); | ||
| boolean terminated = exec.awaitTermination(2, TimeUnit.SECONDS); | ||
| if (!terminated) { | ||
| System.out.println("Executor did not terminate in the given time."); |
There was a problem hiding this comment.
✅ LGTM!
Review Summary
Commits Considered (1)
Files Processed (2)
- leader-followers/pom.xml (1 hunk)
- leader-followers/src/main/java/com/iluwatar/leaderfollowers/App.java (2 hunks)
Actionable Comments (0)
Skipped Comments (1)
-
leader-followers/src/main/java/com/iluwatar/leaderfollowers/App.java [26-36]
enhancement: "Improve logging in finally block."
There was a problem hiding this comment.
✅ LGTM!
Review Summary
Commits Considered (1)
- a758249: Merge branch 'iluwatar:master' into master
Files Processed (0)
Actionable Comments (0)
Skipped Comments (0)
- Defined logger explicitly with LoggerFactory.getLogger(...) - Ensured compatibility with Lombok's @slf4j annotation - Fixed compilation error caused by missing 'log' variable
There was a problem hiding this comment.
✅ LGTM!
Review Summary
Commits Considered (1)
Files Processed (2)
- leader-followers/pom.xml (1 hunk)
- leader-followers/src/main/java/com/iluwatar/leaderfollowers/App.java (2 hunks)
Actionable Comments (0)
Skipped Comments (1)
-
leader-followers/src/main/java/com/iluwatar/leaderfollowers/App.java [26-36]
enhancement: "Improve exception handling and logging in finally block."
|
Hi @iluwatar, thank you for the feedback! ✅ I've addressed your review comment by defining the SLF4J logger explicitly instead of using Lombok's Let me know if there's anything else I should improve 🙌 |
| <dependency> | ||
| <groupId>org.projectlombok</groupId> | ||
| <artifactId>lombok</artifactId> | ||
| <version>1.18.24</version> | ||
| <scope>provided</scope> | ||
| </dependency> |
There was a problem hiding this comment.
The dependency already exists in parent pom.xml, so no need to declare it here
| */ | ||
| @Slf4j | ||
| public class App { | ||
| private static final Logger log = LoggerFactory.getLogger(App.class); |
There was a problem hiding this comment.
No need to declare the logger like this. After inserting the annotation you can just log like LOGGER.info("hello")
There was a problem hiding this comment.
✅ LGTM!
Review Summary
Commits Considered (1)
- 4ef609c: fix: add missing logger definition for SLF4J
Files Processed (1)
- leader-followers/src/main/java/com/iluwatar/leaderfollowers/App.java (2 hunks)
Actionable Comments (0)
Skipped Comments (0)
|
|
✅ Updated code based on review:
Let me know if there's anything else to revise. |
|
Hi @iluwatar! 🙌
All checks have passed ✅ and there are no conflicts with base branch. |
|
Looks good! Thank you for the contribution 🎉 @all-contributors please add @2897robo for code |
|
Thank you so much for reviewing and merging! 🙏 Much appreciated! 🚀 |



What does this PR do?
This pull request addresses a high severity issue (java:S2095) reported by SonarCloud in the
App.javafile of theleader-followersmodule.The issue was:
What was the problem?
The
ExecutorServicecreated usingExecutors.newFixedThreadPool(...)was not guaranteed to shut down properly, which could lead to:What does this PR change?
ExecutorServiceusage inside atry-finallyblockawaitTermination(...)to check whether the shutdown was completed within the timeoutshutdownNow()in thefinallyblockWhy not try-with-resources?
ExecutorServicedoes not implementAutoCloseable, so it cannot be used withtry-with-resources.Hence, we use
try-finallyas the proper alternative here.References
✅ Fixes: #2865
🧹 Impact: Improves resource handling, stability, and SonarCloud quality gates