Groovy类加载器行为
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如何知道源是什么 改变了?
GroovyClassloader behaviour understanding ,
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("groovy");
GroovyScriptEngineImpl groovyEngineImpl = (GroovyScriptEngineImpl) engine;
in a loop,
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));
}
I have a couple of questions regarding the above code:
- In a for loop I'm creating a new groovyclassloder object, and
parsing the groovy script twice. When the loop iterates for the
second time, and tries to parse the groovyscript again, what will
occur ? - What will happen on the second time when another object is created.
Will the classloader manage to get the class form the classpath or
again recompile it again? - When recompile is triggered, how does groovy know what the source is
changed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
s
的类型。如果它是一个文件,它将检查是否需要重新编译,如果不需要,它将使用相同的类。如果它是一个String
或其他东西,那么它必须再次从该字符串重新编译类s
. If it is a file, it will check if it needs a recompile, and if not it will use the same class. If it is aString
or something, then it will have to recompile the class from theis String again