/*
* Copyright 2009-2016 Weibo, Inc.
*
* 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 com.weibo.api.motan.core;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import com.weibo.api.motan.common.MotanConstants;
/**
*
* @author maijunsheng
* @version 创建时间:2013-6-6
*
*/
public class DefaultThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup threadGroup;
private final AtomicInteger currentThreadNumber = new AtomicInteger(1);
private final String namePrefix;
private int priority = Thread.NORM_PRIORITY;
private boolean isDaemon = false;
public DefaultThreadFactory() {
this(MotanConstants.FRAMEWORK_NAME);
}
public DefaultThreadFactory(String prefix) {
this(prefix, false);
}
public DefaultThreadFactory(String prefix, boolean isDaemon) {
this(prefix, isDaemon, Thread.NORM_PRIORITY);
}
public DefaultThreadFactory(String prefix, boolean isDaemon, int priority) {
SecurityManager s = System.getSecurityManager();
this.threadGroup = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
this.namePrefix = prefix + "-" + poolNumber.getAndIncrement() + "-thread-";
this.isDaemon = isDaemon;
this.priority = priority;
}
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(threadGroup, r, namePrefix + currentThreadNumber.getAndIncrement(), 0);
thread.setDaemon(isDaemon);
thread.setPriority(priority);
return thread;
}
}