/*
 *  Copyright (C) 2011-2017 Cojen.org
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.cojen.tupl.io;

import java.util.concurrent.ThreadFactory;

/**
 * Creates daemon threads with a specified name prefix.
 *
 * @author Brian S O'Neill
 */
final class NamedThreadFactory implements ThreadFactory {
    private static int cThreadCounter;

    private final String mPrefix;
    private final ThreadGroup mGroup;

    NamedThreadFactory(String prefix) {
        mPrefix = prefix == null ? "Thread" : prefix;
        SecurityManager sm = System.getSecurityManager();
        mGroup = (sm != null) ? sm.getThreadGroup() : Thread.currentThread().getThreadGroup();
    }

    @Override
    public Thread newThread(Runnable r) {
        int num;
        synchronized (NamedThreadFactory.class) {
            num = ++cThreadCounter;
        }
        Thread t = new Thread(mGroup, r, mPrefix + '-' + (num & 0xffffffffL));
        t.setDaemon(true);
        return t;
    }
}

@@ -947,48 +947,8 @@
 y %7B%0A
-    private static int cThreadCounter;%0A%0A
     
@@ -973,24 +973,24 @@
 ng mPrefix;%0A
+
     private 
@@ -1077,36 +1077,8 @@
 ix =
- prefix == null ? %22Thread%22 :
  pre
@@ -1310,191 +1310,155 @@
     
-int num;%0A        synchronized (NamedThreadFactory.class) %7B%0A            num = ++cThreadCounter;%0A        %7D%0A        Thread t = new Thread(mGroup, r, mPrefix + '-' + (num & 0xffffffffL));
+Thread t = new Thread(mGroup, r);%0A        if (mPrefix != null) %7B%0A            t.setName(mPrefix + '-' + Long.toUnsignedString(t.getId()));%0A        %7D
 %0A   

Simplify ThreadFactory implementation a bit.