欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ThreadFactory

程序员文章站 2022-06-06 12:37:27
...
package com.google.code.yanf4j.util;

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

/**
 * Thread factory for worker thread
 * 
 * @author dennis
 * 
 */
public class WorkerThreadFactory implements ThreadFactory {
	private static final AtomicInteger poolNumber = new AtomicInteger(1);
	private final ThreadGroup group;
	private final AtomicInteger threadNumber = new AtomicInteger(1);
	private final String namePrefix;

	public WorkerThreadFactory(ThreadGroup group, String prefix) {
		if (group == null) {
			SecurityManager s = System.getSecurityManager();
			this.group = (s != null) ? s.getThreadGroup() : Thread
					.currentThread().getThreadGroup();
		} else {
			this.group = group;
		}
		if (prefix == null) {
			this.namePrefix = "pool-" + poolNumber.getAndIncrement()
					+ "-thread-";
		} else {
			this.namePrefix = prefix + "-" + poolNumber.getAndIncrement()
					+ "-thread-";
		}
	}
	
	public WorkerThreadFactory(String prefix){
		this(null,prefix);
	}

	public WorkerThreadFactory() {
		this(null, null);
	}

	public Thread newThread(Runnable r) {
		Thread t = new Thread(this.group, r, this.namePrefix
				+ this.threadNumber.getAndIncrement(), 0);
		t.setDaemon(true);
		if (t.getPriority() != Thread.NORM_PRIORITY) {
			t.setPriority(Thread.NORM_PRIORITY);
		}
		return t;
	}

}


yanf4j 中的襄城工厂类