【问题标题】:Jexl parallel processing exampleJexl 并行处理示例
【发布时间】:2018-02-14 14:17:21
【问题描述】:

我需要在 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');

【问题讨论】:

  • 当你说“它仍在按顺序工作”时,这是什么意思?你是怎么检查的?
  • 我尝试通过日志记录向此方法添加大循环(5 秒)。我在a.someMethod() 日志之后得到了b.someMethod() 日志。
  • 也许可以在utils.sleep 中尝试随机时间(以便 i 和 j 有不同的等待时间)。如果它不能解决问题,我会让 jexl 专业人员帮助你:P
  • 尝试了不同的时间,不同的循环长度。仍然打印Starting i i=1 ... i=100 Ending i Starting j j=1 ... j=250 Ending j

标签: java jexl


【解决方案1】:

好像是注解的例子,在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());
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多