GroovyClassloader 行为理解,
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("groovy");
GroovyScriptEngineImpl groovyEngineImpl = (GroovyScriptEngineImpl) engine;
在一个循环中,
for (int i = 0; i < 10; i++) {
long startTime = System.currentTimeMillis();
classLoader = new GroovyClassLoader(groovyEngineImpl.getClassLoader().getParent());
fileName = fileName + i;
Class groovyClass = classLoader.parseClass(s,fileName);
long endTime = System.currentTimeMillis();
System.out.println("Total elapsed time in execution o " + (endTime-startTime));
startTime = System.currentTimeMillis();
groovyClass = classLoader.parseClass(s,fileName);
endTime = System.currentTimeMillis();
System.out.println("Second Time Total elapsed time in execution o " + (endTime-startTime));
}
关于上面的代码,我有几个问题:
- 在 for 循环中,我创建了一个新的 groovyclassloder 对象,并两次解析 groovy 脚本。当循环第二次迭代并尝试再次解析 groovyscript 时,会发生什么?
- 第二次创建另一个对象时会发生什么。类加载器会设法从类路径中获取类还是再次重新编译它?
- 触发重编译的时候,groovy怎么知道源改了什么?