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

com.github.javaclub.toolbox.thread.ConfigThreadFactory Maven / Gradle / Ivy

package com.github.javaclub.toolbox.thread;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ConfigThreadFactory implements ThreadFactory {
	
	private static Logger log = LoggerFactory.getLogger(ConfigThreadFactory.class);

	private final AtomicLong threadNumber = new AtomicLong(1);

	private final String namePrefix;

	private final boolean daemon;

	private static final ThreadGroup threadGroup = new ThreadGroup("internal");

	public static ThreadGroup getThreadGroup() {
		return threadGroup;
	}

	public static ThreadFactory create(String namePrefix, boolean daemon) {
		return new ConfigThreadFactory(namePrefix, daemon);
	}

	public static boolean waitAllShutdown(int timeoutInMillis) {
		ThreadGroup group = getThreadGroup();
		Thread[] activeThreads = new Thread[group.activeCount()];
		group.enumerate(activeThreads);
		Set alives = new HashSet<>(Arrays.asList(activeThreads));
		Set dies = new HashSet<>();
		log.warn("Application ThreadFactory was destroying!");
		log.info("Current actived internal thread count is: {}", alives.size());
		long expire = System.currentTimeMillis() + timeoutInMillis;
		while (System.currentTimeMillis() < expire) {
			classify(alives, dies, new ClassifyStandard() {
				@Override
				public boolean satisfy(Thread thread) {
					return !thread.isAlive() || thread.isInterrupted() || thread.isDaemon();
				}
			});
			if (alives.size() > 0) {
				log.info("Alive internal threads: {}", alives);
				try {
					TimeUnit.SECONDS.sleep(2);
				} catch (InterruptedException ex) {
					// ignore
				}
			} else {
				log.info("All internal threads are shutdown.");
				return true;
			}
		}
		log.warn("Some internal threads are still alive but expire time has reached, alive threads: {}", alives);
		return false;
	}

	private static interface ClassifyStandard {
		boolean satisfy(T thread);
	}

	private static  void classify(Set src, Set des, ClassifyStandard standard) {
		Set set = new HashSet<>();
		for (T t : src) {
			if (standard.satisfy(t)) {
				set.add(t);
			}
		}
		src.removeAll(set);
		des.addAll(set);
	}

	private ConfigThreadFactory(String namePrefix, boolean daemon) {
		this.namePrefix = namePrefix;
		this.daemon = daemon;
	}

	@Override
	public Thread newThread(Runnable runnable) {
		String prefix = (threadGroup.getName() + "-" + namePrefix);
		Thread thread = new Thread(threadGroup, runnable, prefix + "-" + threadNumber.getAndIncrement());
		thread.setDaemon(daemon);
		if (thread.getPriority() != Thread.NORM_PRIORITY) {
			thread.setPriority(Thread.NORM_PRIORITY);
		}
		return thread;
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy