clean: Thread.sleep() util; demo simpleLoop() w.o. output#17
Conversation
Summary of ChangesHello @vorburger, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances thread management by introducing a new Threads utility class that provides a more robust and safer sleep method. This utility is then integrated into the LongIncrementingTask to standardize sleep operations. Additionally, a new demonstration is included to illustrate the utility's usage in a basic looping scenario. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Pull Request Overview
This PR introduces a Thread.sleep() utility wrapper and demonstrates a simple loop without output functionality. The changes clean up thread sleep handling by consolidating the interruption logic into a reusable utility.
- Adds a new
Threadsutility class with a safersleep()method that properly handlesInterruptedException - Refactors
LongIncrementingTaskto use the new sleep utility and adds a demonstration method - Makes minor formatting and error message improvements in
TaskCallable
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/dev/enola/be/task/demo/Threads.java | New utility class providing a safer Thread.sleep() wrapper with proper interrupt handling |
| src/dev/enola/be/task/demo/LongIncrementingTask.java | Refactored to use new sleep utility and added simpleLoop() demo method |
| src/dev/enola/be/task/TaskCallable.java | Minor formatting improvements and simplified error message |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| public static void main(String[] args) throws InterruptedException { | ||
| // Count to max, with 1ms pause between each increment | ||
| var max = 10000; | ||
| var sleep = Duration.ofMillis(0); |
There was a problem hiding this comment.
[nitpick] Using a sleep duration of 0 milliseconds may not provide meaningful demonstration of the sleep functionality. Consider using a small positive duration like Duration.ofMillis(1) to better demonstrate the sleep behavior.
| var sleep = Duration.ofMillis(0); | |
| var sleep = Duration.ofMillis(1); |
There was a problem hiding this comment.
Code Review
This pull request introduces a Threads.sleep() utility method to centralize sleep logic with proper interrupt handling. It also updates LongIncrementingTask to use this utility and adds a simpleLoop method for demonstration/comparison purposes. The changes are a good step towards cleaner concurrency code. My review includes suggestions to improve the readability of the new demo code and to correct and clarify the Javadoc for the new Threads utility class.
| System.out.println( | ||
| "Looped to " | ||
| + max | ||
| + " with " | ||
| + sleep | ||
| + " sleep, but without output, in " | ||
| + duration); |
There was a problem hiding this comment.
| * as a checked exception. It also checks for negative duration (which the original method just | ||
| * ignores, which could hide bugs), and has an optimizing shortcut for duration 0. |
There was a problem hiding this comment.
The Javadoc states that this wrapper checks for negative duration which the "original method" ignores. This is true for Thread.sleep(long), but Thread.sleep(Duration), which is used here, already throws an IllegalArgumentException for negative durations. The comment could be clarified to avoid confusion, for example by specifying which Thread.sleep overload it refers to.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| * something different from this). | ||
| * | ||
| * @param duration Duration to sleep | ||
| * @throws UncheckedInterruptedException if interrupted |
There was a problem hiding this comment.
The javadoc mentions throwing UncheckedInterruptedException but the actual implementation throws InterruptedException (line 40). The documentation should be updated to reflect the actual exception type thrown.
| * @throws UncheckedInterruptedException if interrupted | |
| * @throws InterruptedException if interrupted |
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| var input = new Input(10000, Duration.ofMillis(1)); | ||
| private static void simpleLoop(long max, Duration sleep) throws InterruptedException { | ||
| var start = Instant.now(); | ||
| for (long i = 0; i < max; i++) Threads.sleep(sleep); |
There was a problem hiding this comment.
The loop calls Threads.sleep() for each iteration without any yield or interrupt check. For large values of max, this could create performance issues and make the method unresponsive to interruption. Consider adding periodic interrupt checks similar to the main execute() method.
| for (long i = 0; i < max; i++) Threads.sleep(sleep); | |
| for (long i = 0; i < max; i++) { | |
| Thread.yield(); | |
| if (Thread.currentThread().isInterrupted()) | |
| throw new InterruptedException("Loop was interrupted"); | |
| Threads.sleep(sleep); | |
| } |
No description provided.