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

io.zulia.util.ZuliaThreadFactory Maven / Gradle / Ivy

There is a newer version: 1.6.4
Show newest version
package io.zulia.util;

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

/**
 *
 * Very similar to NamedThreadFactory in lucene but keeps the client from depending on lucene
 *
 */
public class ZuliaThreadFactory implements ThreadFactory {

	private final AtomicInteger threadCounter = new AtomicInteger(1);
	private final ThreadGroup threadGroup;
	private final String prefix;

	public ZuliaThreadFactory(String prefix) {
		SecurityManager s = System.getSecurityManager();
		threadGroup = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
		this.prefix = prefix;
	}

	@Override
	public Thread newThread(Runnable r) {
		Thread t = new Thread(threadGroup, r, prefix + "-" + threadCounter.getAndIncrement(), 0);

		t.setDaemon(true);

		if (t.getPriority() != Thread.NORM_PRIORITY) {
			t.setPriority(Thread.NORM_PRIORITY);
		}
		return t;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy