MVEL executeExpression函数不能并发

时间:2014-12-30 03:16:24

标签: java mutex mvel

在File2中运行main函数,问题是:线程卡在“rval = MVEL.executeExpression(compiledExpression,vars);” ,10个线程按顺序运行,而不是并行,我想知道为什么会发生这种情况。 PS:我正在使用MVEL 2.2,最新版本

File1中:MVELHelper.java

public class MVELHelper {

private static ParserContext _ctx = new ParserContext(false);

//public static Object execute(String expression, Map<String, Object> vars, Databus databus) throws Exception {
public static Object execute(String expression, Map<String, Object> vars) throws Exception {
    Object rval = null;
    try {
        if(vars == null) {
            rval = MVEL.eval(expression, new HashMap<String,Object>());
        }
        else {
            rval = MVEL.eval(expression, vars);
        }
        return rval;
    }
    catch(Exception e) {
        throw new Exception("MVEL FAILED:"+expression,e);
    }
}


public static Serializable compile(String text, ParserContext ctx)
        throws Exception {
    if(ctx == null) {
        //ctx = _ctx;
        ctx=new ParserContext(false);
    }
    Serializable exp = null;
    try {
        exp = MVEL.compileExpression(text, ctx);
        //exp = MVEL.compileExpression(text);

    } 
    catch (Exception e) {
        throw new Exception("failed to compile expression.", e);
    }
    return exp;
}

public static Object compileAndExecute(String expression, Map<String, Object> vars) throws Exception {
    Object rval = null;
    try {
        Serializable compiledExpression=compile(expression,null);
        System.out.println("[COMPILE OVER, Thread Id="+Thread.currentThread().getId()+"] ");
        if(vars == null) {
            rval=MVEL.executeExpression(compiledExpression, new HashMap<String,Object>());
            //rval = MVEL.eval(exp, new HashMap<String,Object>());
        }
        else {
            //rval=MVEL.executeExpression(compiledExpression, vars,(VariableResolverFactory)null);
            rval=MVEL.executeExpression(compiledExpression, vars);
            //rval = MVEL.eval(expression, vars);
        }
        return rval;
    }
    catch(Exception e) {
        throw new Exception("MVEL FAILED:"+expression,e);
    }
}

}

文件2:ExecThread3.java

public class ExecThread3 implements Runnable{


Map dataMap=null;

public Map getDataMap() {
    return dataMap;
}

public void setDataMap(Map dataMap) {
    this.dataMap = dataMap;
}

@Override
public void run() {

    Map varsMap = new HashMap();
    Map dataMap=new HashMap();
    dataMap.put("count",100);
    varsMap.put("dataMap", dataMap);

    String expression="System.out.println(\"[BEFORE Thread Id=\"+Thread.currentThread().getId()+\"] \"+dataMap.get(\"count\"));"+
            "Thread.sleep(3000);"+
            "System.err.println(\"[AFTER Thread Id=\"+Thread.currentThread().getId()+\"] \"+dataMap.get(\"count\"));";

    try {
        //MVEL.compileExpression(expression);
        MVELHelper.compileAndExecute(expression, varsMap);
    }
    catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    for(int k=0;k<10;k++){

        ExecThread3 execThread=new ExecThread3();

        new Thread(execThread).start();

    }

}

}

0 个答案:

没有答案