1

我需要在 Jexl 中同时执行多个操作。在官方指南中,我找到了注释@parallelhttps ://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');
4

1 回答 1

1

似乎它是一个注释的例子,在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());
        }
    }
于 2018-02-14T06:17:21.077 回答