<commit_before>/*
 *  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 <NAME>
 */
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;
    }
}<commit_msg>Simplify ThreadFactory implementation a bit.<commit_after>/*
 *  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 <NAME>
 */
final class NamedThreadFactory implements ThreadFactory {
    private final String mPrefix;
    private final ThreadGroup mGroup;

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

    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(mGroup, r);
        if (mPrefix != null) {
            t.setName(mPrefix + '-' + Long.toUnsignedString(t.getId()));
        }
        t.setDaemon(true);
        return t;
    }
}
{'fraction_non_alphanumeric': 0.08695652173913043, 'fraction_numerical': 0.0059430716296528, 'mean_word_length': 3.068702290076336, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1458604', 'n_tokens_mistral': 891, 'n_tokens_neox': 862, 'n_words': 343}