似乎它是一个注释的例子,在jexl 代码中似乎只有synchronized
实现。
@Override
public Object processAnnotation(String name, Object[] args,Callable<Object> statement) throws Exception {
if ("synchronized".equals(name)) {
final Object arg = args[0];
synchronized(arg) {
return statement.call();
}
}
throw new IllegalArgumentException("unknown annotation " + name);
}
有一个并行测试,但它使用ExecutorService
:
/**
* Run same test function in NTHREADS in parallel.
* @param ctask the task / test
* @param loops number of loops to perform
* @param cache whether jexl cache is used or not
* @throws Exception if anything goes wrong
*/
@SuppressWarnings("boxing")
void runThreaded(Class<? extends Task> ctask, int loops, boolean cache) throws Exception {
if (loops == 0) {
loops = MIX.length;
}
if (!cache) {
jexl = jexlNoCache;
} else {
jexl = jexlCache;
}
java.util.concurrent.ExecutorService execs = java.util.concurrent.Executors.newFixedThreadPool(NTHREADS);
List<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>(NTHREADS);
for (int t = 0; t < NTHREADS; ++t) {
tasks.add(jexl.newInstance(ctask, loops));
}
// let's not wait for more than a minute
List<Future<Integer>> futures = execs.invokeAll(tasks, 60, TimeUnit.SECONDS);
// check that all returned loops
for (Future<Integer> future : futures) {
Assert.assertEquals(Integer.valueOf(loops), future.get());
}
}