All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.voovan.tools.threadpool.DefaultThreadFactory Maven / Gradle / Ivy

There is a newer version: 4.3.8
Show newest version
package org.voovan.tools.threadpool;

import org.voovan.tools.FastThread;

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

/**
 * 类文字命名
 *
 * @author: helyho
 * Voovan Framework.
 * WebSite: https://github.com/helyho/Voovan
 * Licence: Apache v2 License
 */
public class DefaultThreadFactory implements ThreadFactory {
    private String name;
    private AtomicInteger threadCount = new AtomicInteger(0);
    private boolean daemon;
    private int priority;

    public DefaultThreadFactory(String name) {
        this(name, false, 5);
    }

    public DefaultThreadFactory(String name, boolean daemon) {
        this(name, daemon, 5);
    }

    public DefaultThreadFactory(String name, boolean daemon, int priority) {
        this.name = name;
        this.daemon = daemon;
        this.priority = priority;
    }

    @Override
    public Thread newThread(Runnable r) {
        FastThread fastThread = new FastThread(r, name + "-" + threadCount.getAndIncrement());

        try {
            if (fastThread.isDaemon()) {
                if (!this.daemon) {
                    fastThread.setDaemon(false);
                }
            } else if (this.daemon) {
                fastThread.setDaemon(true);
            }

            if (fastThread.getPriority() != this.priority) {
                fastThread.setPriority(this.priority);
            }
        } catch (Exception e) {
        }

        return fastThread;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy