com.alibaba.ocean.rawsdk.client.util.NamedThreadFactory Maven / Gradle / Ivy
/**
* Project: ocean.client.java.basic
*
* File Created at 2011-10-25
* $Id: NamedThreadFactory.java 311300 2013-12-23 06:15:28Z yichun.wangyc $
*
* Copyright 2008 Alibaba.com Croporation Limited.
* All rights reserved.
*
* This software is the confidential and proprietary information of
* Alibaba Company. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Alibaba.com.
*/
package com.alibaba.ocean.rawsdk.client.util;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Comment of NamedThreadFactory
* @author jade
*
*/
public class NamedThreadFactory implements ThreadFactory {
static final AtomicInteger poolNumber = new AtomicInteger(1);
final AtomicInteger threadNumber = new AtomicInteger(1);
final ThreadGroup group;
final String namePrefix;
final boolean isDaemon;
public NamedThreadFactory() {
this("timor.pool");
}
public NamedThreadFactory(String name) {
this(name, false);
}
public NamedThreadFactory(String preffix, boolean daemon) {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
namePrefix = preffix + "-" + poolNumber.getAndIncrement() + "-thread-";
isDaemon = daemon;
}
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
t.setDaemon(isDaemon);
if (t.getPriority() != Thread.NORM_PRIORITY) {
t.setPriority(Thread.NORM_PRIORITY);
}
return t;
}
}