package io.vertx.core.impl;
import io.vertx.core.VertxOptions;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class VertxThreadFactory implements ThreadFactory {
private static final Object FOO = new Object();
private static Map<VertxThread, Object> weakMap = new WeakHashMap<>();
private static synchronized void addToMap(VertxThread thread) {
weakMap.put(thread, FOO);
}
private final String prefix;
private final AtomicInteger threadCount = new AtomicInteger(0);
private final BlockedThreadChecker checker;
private final boolean worker;
private final long maxExecTime;
private final TimeUnit maxExecTimeUnit;
VertxThreadFactory(String prefix, BlockedThreadChecker checker, boolean worker, long maxExecTime, TimeUnit maxExecTimeUnit) {
this.prefix = prefix;
this.checker = checker;
this.worker = worker;
this.maxExecTime = maxExecTime;
this.maxExecTimeUnit = maxExecTimeUnit;
}
public static synchronized void unsetContext(ContextImpl ctx) {
for (VertxThread thread: weakMap.keySet()) {
if (thread.getContext() == ctx) {
thread.setContext(null);
}
}
}
public Thread newThread(Runnable runnable) {
VertxThread t = new VertxThread(runnable, prefix + threadCount.getAndIncrement(), worker, maxExecTime, maxExecTimeUnit);
if (checker != null) {
checker.registerThread(t, t);
}
addToMap(t);
t.setDaemon(false);
return t;
}
}