<bdo id='AwzJF'></bdo><ul id='AwzJF'></ul>

<small id='AwzJF'></small><noframes id='AwzJF'>

    1. <i id='AwzJF'><tr id='AwzJF'><dt id='AwzJF'><q id='AwzJF'><span id='AwzJF'><b id='AwzJF'><form id='AwzJF'><ins id='AwzJF'></ins><ul id='AwzJF'></ul><sub id='AwzJF'></sub></form><legend id='AwzJF'></legend><bdo id='AwzJF'><pre id='AwzJF'><center id='AwzJF'></center></pre></bdo></b><th id='AwzJF'></th></span></q></dt></tr></i><div id='AwzJF'><tfoot id='AwzJF'></tfoot><dl id='AwzJF'><fieldset id='AwzJF'></fieldset></dl></div>

      1. <tfoot id='AwzJF'></tfoot>

        <legend id='AwzJF'><style id='AwzJF'><dir id='AwzJF'><q id='AwzJF'></q></dir></style></legend>

        为什么使用invokedynamic 调用Java 8 lambda?

        时间:2023-08-25

              <tbody id='36oNP'></tbody>
            • <bdo id='36oNP'></bdo><ul id='36oNP'></ul>
              1. <legend id='36oNP'><style id='36oNP'><dir id='36oNP'><q id='36oNP'></q></dir></style></legend>
                <tfoot id='36oNP'></tfoot>
                • <i id='36oNP'><tr id='36oNP'><dt id='36oNP'><q id='36oNP'><span id='36oNP'><b id='36oNP'><form id='36oNP'><ins id='36oNP'></ins><ul id='36oNP'></ul><sub id='36oNP'></sub></form><legend id='36oNP'></legend><bdo id='36oNP'><pre id='36oNP'><center id='36oNP'></center></pre></bdo></b><th id='36oNP'></th></span></q></dt></tr></i><div id='36oNP'><tfoot id='36oNP'></tfoot><dl id='36oNP'><fieldset id='36oNP'></fieldset></dl></div>

                  <small id='36oNP'></small><noframes id='36oNP'>

                • 本文介绍了为什么使用invokedynamic 调用Java 8 lambda?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  invokedynamic 指令用于帮助 VM 在运行时确定方法引用,而不是在编译时对其进行硬接线.

                  The invokedynamic instruction is used to help the VM determine the method reference at runtime instead hardwiring it at compile time.

                  这对于在运行时才知道确切方法和参数类型的动态语言很有用.但 Java lambda 并非如此.它们被转换为具有明确定义的参数的静态方法.并且可以使用 invokestatic 调用此方法.

                  This is useful with dynamic languages where the exact method and argument types aren't known until runtime. But that isn't the case with Java lambdas. They are translated to a static method with well defined arguments. And this method can be invoked using invokestatic.

                  那么 invokedynamic 对 lambda 有什么需求,尤其是在性能受到影响的情况下?

                  So then what is the need of invokedynamic for lambdas, especially when there is a performance hit?

                  推荐答案

                  Lambda 不是使用 invokedynamic 调用的,它们的对象表示是使用 invokedynamic 创建的,实际调用是一个常规的 invokevirtualinvokeinterface.

                  Lambdas are not invoked using invokedynamic, their object representation is created using invokedynamic, the actual invocation is a regular invokevirtual or invokeinterface.

                  例如:

                  // creates an instance of (a subclass of) Consumer 
                  // with invokedynamic to java.lang.invoke.LambdaMetafactory 
                  something(x -> System.out.println(x));   
                  
                  void something(Consumer<String> consumer) {
                        // invokeinterface
                        consumer.accept("hello"); 
                  }
                  

                  任何 lambda 都必须成为某个基类或接口的实例.该实例有时会包含从原始方法捕获的变量的副本,有时还会包含指向父对象的指针.这可以实现为匿名类.

                  Any lambda has to become an instance of some base class or interface. That instance will sometimes contain a copy of the variables captured from the original method and sometimes a pointer to the parent object. This can be implemented as an anonymous class.

                  为什么调用dynamic

                  简短的回答是:在运行时生成代码.

                  The short answer is: to generate code in runtime.

                  Java 维护人员选择在运行时生成实现类.这是通过调用 java.lang.invoke.LambdaMetafactory.metafactory 来完成的.由于该调用的参数(返回类型、接口和捕获的参数)可以更改,因此需要 invokedynamic.

                  The Java maintainers chose to generate the implementation class in runtime. This is done by calling java.lang.invoke.LambdaMetafactory.metafactory. Since the arguments for that call (return type, interface, and captured parameters) can change, this requires invokedynamic.

                  使用 invokedynamic 在运行时构造匿名类,允许 JVM 在运行时生成该类字节码.对同一语句的后续调用使用缓存版本.使用 invokedynamic 的另一个原因是能够在将来更改实现策略,而无需更改已编译的代码.

                  Using invokedynamic to construct the anonymous class in runtime, allows the JVM to generate that class bytecode in runtime. The subsequent calls to the same statement use a cached version. The other reason to use invokedynamic is to be able to change the implementation strategy in the future without having to change already compiled code.

                  未走的路

                  另一个选项是编译器为每个 lambda 实例创建一个内部类,相当于将上面的代码翻译成:

                  The other option would be the compiler creating an innerclass for each lambda instantiation, equivalent to translating the above code into:

                  something(new Consumer() { 
                      public void accept(x) {
                         // call to a generated method in the base class
                         ImplementingClass.this.lambda$1(x);
                  
                         // or repeating the code (awful as it would require generating accesors):
                         System.out.println(x);
                      }
                  );   
                  

                  这需要在编译时创建类,然后在运行时加载.jvm 工作这些类的方式将驻留在与原始类相同的目录中.第一次执行使用该 lambda 的语句时,必须加载和初始化该匿名类.

                  This requires creating classes in compile time and having to load then during runtime. The way jvm works those classes would reside in the same directory as the original class. And the first time you execute the statement that uses that lambda, that anonymous class would have to be loaded and initialized.

                  关于性能

                  第一次调用 invokedynamic 将触发匿名类生成.然后将操作码 invokedynamic 替换为 code 这在性能上等同于手动编写匿名实例化.

                  The first call to invokedynamic will trigger the anonymous class generation. Then the opcode invokedynamic is replaced with code that's equivalent in performance to the writing manually the anonymous instantiation.

                  这篇关于为什么使用invokedynamic 调用Java 8 lambda?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:区分 Java 线程和 OS 线程? 下一篇:JVM 如何确保 System.identityHashCode() 永远不会改变?

                  相关文章

                  <legend id='o4gBf'><style id='o4gBf'><dir id='o4gBf'><q id='o4gBf'></q></dir></style></legend>

                  <i id='o4gBf'><tr id='o4gBf'><dt id='o4gBf'><q id='o4gBf'><span id='o4gBf'><b id='o4gBf'><form id='o4gBf'><ins id='o4gBf'></ins><ul id='o4gBf'></ul><sub id='o4gBf'></sub></form><legend id='o4gBf'></legend><bdo id='o4gBf'><pre id='o4gBf'><center id='o4gBf'></center></pre></bdo></b><th id='o4gBf'></th></span></q></dt></tr></i><div id='o4gBf'><tfoot id='o4gBf'></tfoot><dl id='o4gBf'><fieldset id='o4gBf'></fieldset></dl></div>

                      <small id='o4gBf'></small><noframes id='o4gBf'>

                      • <bdo id='o4gBf'></bdo><ul id='o4gBf'></ul>
                    1. <tfoot id='o4gBf'></tfoot>