gpt4 book ai didi

java - Jexl并行处理示例

转载 作者:行者123 更新时间:2023-11-30 06:18:15 25 4
gpt4 key购买 nike

我需要在 Jexl 中同时执行多个操作。在官方指南中我发现注释@parallel: https://commons.apache.org/proper/commons-jexl/reference/syntax.html但我没有找到任何如何使用它的示例。

谁能提供一些例子吗?

我认为它会像这样工作:

@parallel { a.someMethod() }
@parallel { b.someMethod() }

但似乎它仍在按顺序工作。我尝试过的第二个例子,仍然不起作用:

var loopFunction = function(title){
var i = 0;
logger:info("Starting "+title);
while(i<100) {
logger:info(title+"="+i);
utils:sleep(25);
i += 1;
}
logger:info("Ending "+title);
}

@parallel loopFunction('i');
@parallel loopFunction('j');

最佳答案

看起来是一个注释的例子,在jexl code似乎只实现了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());
}
}

关于java - Jexl并行处理示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48769921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com