/*
* (C) Copyright 2015-2016 the original author or authors.
*
* 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.
*
* Contributors:
* ohun@live.cn (夜色)
*/
package com.mpush.tools.thread;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
public final class NamedPoolThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNum = new AtomicInteger(1);
private final AtomicInteger threadNum = new AtomicInteger(1);
private final ThreadGroup group;
private final String namePre;
private final boolean isDaemon;
public NamedPoolThreadFactory(String prefix) {
this(prefix, true);
}
public NamedPoolThreadFactory(String prefix, boolean daemon) {
SecurityManager manager = System.getSecurityManager();
if (manager != null) {
group = manager.getThreadGroup();
} else {
group = Thread.currentThread().getThreadGroup();
}
isDaemon = daemon;
namePre = prefix + "-p-" + poolNum.getAndIncrement() + "-t-";
}
/**
* stackSize - 新线程的预期堆栈大小,为零时表示忽略该参数
*/
@Override
public Thread newThread(Runnable runnable) {
Thread t = new Thread(group, runnable, namePre + threadNum.getAndIncrement(), 0);
t.setContextClassLoader(NamedPoolThreadFactory.class.getClassLoader());
t.setPriority(Thread.NORM_PRIORITY);
t.setDaemon(isDaemon);
return t;
}
}