Groovy类加载器行为

发布于 2025-01-01 16:41:53 字数 1237 浏览 5 评论 0原文

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));


}

我对上述代码有几个问题:

  1. 在 for 循环中,我正在创建一个新的 groovyclassloder 对象,并且 解析 groovy 脚本两次。当循环迭代时 第二次,并尝试再次解析 groovyscript,会发生什么 发生 ?
  2. 当另一个对象第二次创建时会发生什么。 类加载器是否会设法从类路径中获取类? 再次重新编译一遍?
  3. 当重新编译被触发时,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:

  1. 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 ?
  2. 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?
  3. When recompile is triggered, how does groovy know what the source is
    changed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池予 2025-01-08 16:41:53
  1. 每次循环时,您都会丢弃类加载器并创建一个新的类加载器。这个新的类加载器将不知道您丢弃的类加载器加载的类
  2. ,这取决于 s 的类型。如果它是一个文件,它将检查是否需要重新编译,如果不需要,它将使用相同的类。如果它是一个 String 或其他东西,那么它必须再次从该字符串重新编译类
  3. https://github.com/groovy/groovy-core/blob/master/src/main/groovy/lang/GroovyClassLoader.java#L845
  1. Each time round the loop you are throwing away the classloader and creating a new one. This new classLoader will have no knowledge of classes loaded by the classLoader you have thrown away
  2. It depends on the type of 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 a String or something, then it will have to recompile the class from theis String again
  3. https://github.com/groovy/groovy-core/blob/master/src/main/groovy/lang/GroovyClassLoader.java#L845
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文