-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added DaemonThreadFactory to avoid hanging in the tehuti application (#…
…29) * Added DaemonThreadFactory to avoid hanging in the tehuti application Previously, 'RedundantLogFilter' is using non-daemon thread to clean up the log filter, and it would result in stuck application if the application doesn't invoke 'RedundantLogFilter#shutdown', which is error prone. This PR adds a 'DaemonThreadFactory', so that the log filter threads won't block Tehuti application shutdown even it doesn't invoke 'RedundantLogFilter#shutdown' explicitly. * Updated javadoc
- Loading branch information
Showing
4 changed files
with
43 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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,22 @@ | ||
package io.tehuti.utils; | ||
|
||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
|
||
public class DaemonThreadFactory implements ThreadFactory { | ||
private final AtomicInteger threadNumber; | ||
private final String namePrefix; | ||
|
||
public DaemonThreadFactory(String threadNamePrefix) { | ||
this.threadNumber = new AtomicInteger(1); | ||
this.namePrefix = threadNamePrefix; | ||
} | ||
|
||
public Thread newThread(Runnable r) { | ||
Thread t = new Thread(r, namePrefix + "-t" + threadNumber.getAndIncrement()); | ||
t.setDaemon(true); | ||
return t; | ||
} | ||
|
||
} |
This file contains 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 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,15 @@ | ||
package io.tehuti.utils; | ||
|
||
|
||
public class RedundantLogFilterTest { | ||
|
||
/** | ||
* Junit test shutdown will force kill all the live threads (non-daemon), so that here we use a cmd application | ||
* to validate whether {@link RedundantLogFilter} would stuck or not when {@link RedundantLogFilter#shutdown()} is | ||
* not explicitly invoked. | ||
*/ | ||
public static void main(String[] args) throws InterruptedException { | ||
RedundantLogFilter logFilter = new RedundantLogFilter(10, 1000); | ||
logFilter.isRedundantLog("test"); | ||
} | ||
} |