Skip to content
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

feature: add prefix counter for NamedThreadFactory #1710

Merged
merged 13 commits into from
Oct 21, 2019
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.seata.common.thread;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

Expand All @@ -24,9 +26,10 @@
* The type Named thread factory.
*
* @author jimin.jm @alibaba-inc.com
* @date 2018 /9/12
* @author ggndnn
*/
public class NamedThreadFactory implements ThreadFactory {
private final static Map<String, AtomicInteger> PREFIX_COUNTER = new ConcurrentHashMap<>();
jsbxyyx marked this conversation as resolved.
Show resolved Hide resolved
private final AtomicInteger counter = new AtomicInteger(0);
private final String prefix;
private final int totalSize;
Expand All @@ -40,7 +43,9 @@ public class NamedThreadFactory implements ThreadFactory {
* @param makeDaemons the make daemons
*/
public NamedThreadFactory(String prefix, int totalSize, boolean makeDaemons) {
this.prefix = prefix;
PREFIX_COUNTER.putIfAbsent(prefix, new AtomicInteger(0));
int prefixCounter = PREFIX_COUNTER.get(prefix).incrementAndGet();
this.prefix = prefix + "_" + prefixCounter;
this.makeDaemons = makeDaemons;
this.totalSize = totalSize;
}
Expand Down