-
Notifications
You must be signed in to change notification settings - Fork 50
feat!: 1633 use descriptive thread names #1704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jarebudev
wants to merge
4
commits into
open-feature:main
Choose a base branch
from
jarebudev:1633_use_descriptive_thread_names
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
120e11e
added a configurable thread factory to allow naming of threads
jarebudev fadc23c
Merge branch 'main' into 1633_use_descriptive_thread_names
jarebudev 7286fa5
tidy up
jarebudev 2629c5e
Merge branch 'main' into 1633_use_descriptive_thread_names
jarebudev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/dev/openfeature/sdk/internal/ConfigurableThreadFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package dev.openfeature.sdk.internal; | ||
|
|
||
| import java.util.concurrent.ThreadFactory; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| /** | ||
| * A configurable thread factory for internal use in the SDK. | ||
| * Allows daemon or non-daemon threads to be created with a custom name prefix. | ||
| */ | ||
| public final class ConfigurableThreadFactory implements ThreadFactory { | ||
|
|
||
| private final AtomicInteger counter = new AtomicInteger(); | ||
| private final String namePrefix; | ||
| private final boolean daemon; | ||
|
|
||
| /** | ||
| * {@link ConfigurableThreadFactory}'s constructor. | ||
| * | ||
| * @param namePrefix Prefix used for setting the new thread's name. | ||
| */ | ||
| public ConfigurableThreadFactory(String namePrefix) { | ||
| this(namePrefix, false); | ||
| } | ||
|
|
||
| /** | ||
| * {@link ConfigurableThreadFactory}'s constructor. | ||
| * | ||
| * @param namePrefix Prefix used for setting the new thread's name. | ||
| * @param daemon Whether daemon or non-daemon threads will be created. | ||
| */ | ||
| public ConfigurableThreadFactory(String namePrefix, boolean daemon) { | ||
| this.namePrefix = namePrefix; | ||
| this.daemon = daemon; | ||
| } | ||
|
|
||
| @Override | ||
| public Thread newThread(Runnable runnable) { | ||
| final Thread thread = new Thread(runnable); | ||
| thread.setDaemon(daemon); | ||
| thread.setName(namePrefix + "-" + counter.incrementAndGet()); | ||
| return thread; | ||
| } | ||
| } | ||
42 changes: 42 additions & 0 deletions
42
src/test/java/dev/openfeature/sdk/internal/ConfigurableThreadFactoryTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package dev.openfeature.sdk.internal; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class ConfigurableThreadFactoryTest { | ||
|
|
||
| private static final String THREAD_NAME = "testthread"; | ||
| private final Runnable runnable = () -> {}; | ||
|
|
||
| @Test | ||
| void verifyNewThreadHasNamePrefix() { | ||
|
|
||
| var configurableThreadFactory = new ConfigurableThreadFactory(THREAD_NAME); | ||
| var thread = configurableThreadFactory.newThread(runnable); | ||
|
|
||
| assertThat(thread.getName()).isEqualTo(THREAD_NAME + "-1"); | ||
| assertThat(thread.isDaemon()).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| void verifyNewThreadHasNamePrefixWithIncrement() { | ||
|
|
||
| var configurableThreadFactory = new ConfigurableThreadFactory(THREAD_NAME); | ||
| var threadOne = configurableThreadFactory.newThread(runnable); | ||
| var threadTwo = configurableThreadFactory.newThread(runnable); | ||
|
|
||
| assertThat(threadOne.getName()).isEqualTo(THREAD_NAME + "-1"); | ||
| assertThat(threadTwo.getName()).isEqualTo(THREAD_NAME + "-2"); | ||
| } | ||
|
|
||
| @Test | ||
| void verifyNewDaemonThreadHasNamePrefix() { | ||
|
|
||
| var configurableThreadFactory = new ConfigurableThreadFactory(THREAD_NAME, true); | ||
| var thread = configurableThreadFactory.newThread(runnable); | ||
|
|
||
| assertThat(thread.getName()).isEqualTo(THREAD_NAME + "-1"); | ||
| assertThat(thread.isDaemon()).isTrue(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For improved robustness, it's a good practice to validate constructor parameters. The
namePrefixshould not be null or empty to prevent creating threads with malformed names (e.g.,-1). Adding a check will make this utility class safer to use in the future.You could also add a corresponding test case to verify this behavior: