/*
 *  Copyright 1999-2019 Seata.io Group.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package io.seata.common.thread;

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

import io.netty.util.concurrent.FastThreadLocalThread;

/**
 * The type Named thread factory.
 *
 * @author jimin.jm @alibaba-inc.com
 * @date 2018 /9/12
 */
public class NamedThreadFactory implements ThreadFactory {
    private final AtomicInteger counter = new AtomicInteger(0);
    private final String prefix;
    private final int totalSize;
    private final boolean makeDaemons;

    /**
     * Instantiates a new Named thread factory.
     *
     * @param prefix      the prefix
     * @param totalSize   the total size
     * @param makeDaemons the make daemons
     */
    public NamedThreadFactory(String prefix, int totalSize, boolean makeDaemons) {
        this.prefix = prefix;
        this.makeDaemons = makeDaemons;
        this.totalSize = totalSize;
    }

    /**
     * Instantiates a new Named thread factory.
     *
     * @param prefix      the prefix
     * @param makeDaemons the make daemons
     */
    public NamedThreadFactory(String prefix, boolean makeDaemons) {
        this(prefix, 0, makeDaemons);
    }

    /**
     * Instantiates a new Named thread factory.
     *
     * @param prefix    the prefix
     * @param totalSize the total size
     */
    public NamedThreadFactory(String prefix, int totalSize) {
        this(prefix, totalSize, true);
    }

    @Override
    public Thread newThread(Runnable r) {
        String name = prefix + "_" + counter.incrementAndGet();
        if (totalSize > 1) {
            name += "_" + totalSize;
        }
        Thread thread = new FastThreadLocalThread(r, name);

        thread.setDaemon(makeDaemons);
        if (thread.getPriority() != Thread.NORM_PRIORITY) {
            thread.setPriority(Thread.NORM_PRIORITY);
        }
        return thread;
    }
}

@@ -641,16 +641,85 @@
 hread;%0A%0A
+import java.util.Map;%0Aimport java.util.concurrent.ConcurrentHashMap;%0A
 import j
@@ -753,16 +753,16 @@
 actory;%0A
-
 import j
@@ -946,23 +946,21 @@
  * @
-date 2018 /9/12
+author ggndnn
 %0A */
@@ -1019,16 +1019,112 @@
 ctory %7B%0A
+    private final static Map%3CString, AtomicInteger%3E PREFIX_COUNTER = new ConcurrentHashMap%3C%3E();%0A
     priv
@@ -1552,32 +1552,172 @@
  makeDaemons) %7B%0A
+        PREFIX_COUNTER.putIfAbsent(prefix, new AtomicInteger(0));%0A        int prefixCounter = PREFIX_COUNTER.get(prefix).incrementAndGet();%0A
         this.pre
@@ -1724,24 +1724,46 @@
 fix = prefix
+ + %22_%22 + prefixCounter
 ;%0A        th

verify that the analysis of the cause of issue 1702 is correct (#1710)