mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-27 21:01:15 +08:00
Extract class for Thread Safety checks
This commit is contained in:
parent
64a05e7b1f
commit
0d2a6e4eba
@ -9,6 +9,8 @@ import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.reflections.util.ClasspathHelper;
|
||||
|
||||
import com.querydsl.core.testutil.ThreadSafety;
|
||||
|
||||
public class CycleClassInitDependencyTest {
|
||||
|
||||
private static ClassLoader loader;
|
||||
@ -27,16 +29,12 @@ public class CycleClassInitDependencyTest {
|
||||
}
|
||||
|
||||
@Test(timeout = 2000)
|
||||
public void test() throws InterruptedException {
|
||||
public void test() {
|
||||
|
||||
// each thread wants to load one part of the dependency circle
|
||||
Thread t1 = new Thread(new LoadClassRunnable("com.querydsl.core.types.dsl.NumberExpression"));
|
||||
Thread t2 = new Thread(new LoadClassRunnable("com.querydsl.core.types.dsl.Expressions"));
|
||||
t1.start();
|
||||
t2.start();
|
||||
|
||||
t1.join();
|
||||
|
||||
ThreadSafety.check(
|
||||
new LoadClassRunnable("com.querydsl.core.types.dsl.NumberExpression"),
|
||||
new LoadClassRunnable("com.querydsl.core.types.dsl.Expressions"));
|
||||
}
|
||||
|
||||
private static class LoadClassRunnable implements Runnable {
|
||||
|
||||
@ -0,0 +1,35 @@
|
||||
package com.querydsl.core.testutil;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.util.concurrent.Futures;
|
||||
import com.google.common.util.concurrent.MoreExecutors;
|
||||
|
||||
public final class ThreadSafety {
|
||||
|
||||
private ThreadSafety() { }
|
||||
|
||||
public static void check(Runnable... runnables) {
|
||||
Collection<Callable<Object>> callables = Lists.newArrayListWithCapacity(runnables.length);
|
||||
ExecutorService executor = MoreExecutors.getExitingScheduledExecutorService(
|
||||
new ScheduledThreadPoolExecutor(runnables.length));
|
||||
|
||||
for (Runnable runnable : runnables) {
|
||||
callables.add(Executors.callable(runnable));
|
||||
}
|
||||
List<Future<Object>> result;
|
||||
try {
|
||||
result = executor.invokeAll(callables);
|
||||
} catch (InterruptedException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
|
||||
for (Future<Object> each : result) {
|
||||
// all need to complete successfully
|
||||
Futures.getUnchecked(each);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user