package org.testng.internal.thread;
import org.testng.collections.Lists;
import org.testng.internal.Utils;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadUtil {
public static final String THREAD_NAME = "TestNG";
public static boolean isTestNGThread() {
return Thread.currentThread().getName().contains(THREAD_NAME);
}
public static void execute(String name, List<? extends Runnable> tasks, int threadPoolSize,
long timeout, boolean triggerAtOnce) {
Utils.log("ThreadUtil", 2, "Starting executor timeOut:" + timeout + "ms"
+ " workers:" + tasks.size() + " threadPoolSize:" + threadPoolSize);
ExecutorService pooledExecutor = new ThreadPoolExecutor(
threadPoolSize, threadPoolSize, timeout, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(), new TestNGThreadFactory(name));
List<Callable<Object>> callables = Lists.newArrayList();
for (final Runnable task : tasks) {
callables.add(new Callable<Object>() {
@Override
public Object call() throws Exception {
task.run();
return null;
}
});
}
try {
if (timeout != 0) {
pooledExecutor.invokeAll(callables, timeout, TimeUnit.MILLISECONDS);
} else {
pooledExecutor.invokeAll(callables);
}
} catch (InterruptedException handled) {
handled.printStackTrace();
Thread.currentThread().interrupt();
} finally {
pooledExecutor.shutdown();
}
}
public static String currentThreadInfo() {
Thread thread= Thread.currentThread();
return String.valueOf(thread.getName() + "@" + thread.hashCode());
}
public static IExecutor createExecutor(int threadCount, String threadFactoryName) {
return new ExecutorAdapter(threadCount, createFactory(threadFactoryName));
}
private static IThreadFactory createFactory(String name) {
return new ThreadFactoryImpl(name);
}
public static class ThreadFactoryImpl extends TestNGThreadFactory implements IThreadFactory {
private final List<Thread> threads = Lists.newArrayList();
public ThreadFactoryImpl(String name) {
super("method=" + name);
}
@Override
public Object getThreadFactory() {
return this;
}
@Override
public Thread newThread(Runnable r) {
Thread t = super.newThread(r);
threads.add(t);
return t;
}
@Override
public List<Thread> getThreads() {
return threads;
}
}
}