-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
54 additions
and
14 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
41 changes: 41 additions & 0 deletions
41
common/src/main/java/com/turikhay/mc/mapmodcompanion/DaemonThreadFactory.java
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,41 @@ | ||
package com.turikhay.mc.mapmodcompanion; | ||
|
||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class DaemonThreadFactory implements ThreadFactory { | ||
private final AtomicInteger counter = new AtomicInteger(); | ||
private final ILogger logger; | ||
private final String name; | ||
private final Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() { | ||
@Override | ||
public void uncaughtException(Thread thread, Throwable throwable) { | ||
logger.error("Error executing the task inside " + thread.getName(), throwable); | ||
} | ||
}; | ||
|
||
public DaemonThreadFactory(ILogger logger, String name) { | ||
this.logger = logger; | ||
this.name = name; | ||
} | ||
|
||
public DaemonThreadFactory(ILogger logger, Class<?> cl) { | ||
this(logger, cl.getSimpleName()); | ||
} | ||
|
||
@Override | ||
public Thread newThread(Runnable runnable) { | ||
Thread t = new Thread(runnable, computeNextName()); | ||
t.setDaemon(true); | ||
t.setUncaughtExceptionHandler(handler); | ||
return t; | ||
} | ||
|
||
private String computeNextName() { | ||
int i = counter.getAndIncrement(); | ||
if (i == 0) { | ||
return name; | ||
} | ||
return name + "#" + i; | ||
} | ||
} |
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
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