gpt4 book ai didi

scala - 在斯卡拉;我应该使用App特性吗?

转载 作者:行者123 更新时间:2023-12-03 08:49:33 25 4
gpt4 key购买 nike

我刚刚开始学习Scala,我正在关注的许多教程都将main方法的不同表示形式结合使用。除了熟悉的主要方法外,还使用了特质AppApplication
看来Application已被弃用,不建议使用,但我找不到任何有关这些定义入口点的方法的详细说明。

因此,我想知道是否有人可以向我解释:

  • 特质AppApplication如何工作?
  • 为什么不再建议使用Application特性,而App特性又有什么不同呢?
  • 我应该在哪里使用传统的main方法,什么时候应该使用App启动程序?两种方法有什么区别?
  • 最佳答案

    Application 特征的问题实际上在其文档中进行了描述:

    (1) Threaded code that references the object will block until static initialization is complete. However, because the entire execution of an object extending Application takes place during static initialization, concurrent code will always deadlock if it must synchronize with the enclosing object.



    这是一个棘手的问题。如果扩展 Application特性,则基本上是在创建Java类:
    class MyApplication implements Application {
    static {
    // All code goes in here
    }
    }

    JVM在 MyApplication类上隐式同步运行上述类初始化器。这样,可以确保在初始化 MyApplication的类之前不会创建任何实例。如果您从应用程序中产生一个再次需要访问 MyApplication实例的线程,则您的应用程序将死锁,因为类初始化仅在整个程序执行后才完成。这意味着一个悖论,因为只要您的程序正在运行,就无法创建任何实例。

    (2) As described above, there is no way to obtain the command-line arguments because all code in body of an object extending Application is run as part of the static initialization which occurs before Application's main method even begins execution.



    类初始化器不接受任何参数。另外,它会先运行,然后再将任何值传递给该类,因为在甚至可以分配一个静态字段值之前,都需要执行该类初始化程序。因此,您通常在 args方法上收到的 main丢失了。

    (3) Static initializers are run only once during program execution, and JVM authors usually assume their execution to be relatively short. Therefore, certain JVM configurations may become confused, or simply fail to optimize or JIT the code in the body of an object extending Application. This can lead to a significant performance degradation.



    JVM优化经常运行的代码。通过这种方式,可以确保不会浪费时间,而这实际上并不是性能瓶颈。但是,它可以安全地假定 static方法仅执行一次,因为它们无法手动调用。因此,它不会优化从类初始化程序运行的代码,但是,如果您正在使用 main特性,则它是应用程序的 Application方法代码。

    App 特性通过扩展 DelayedInit 修复了所有问题。 Scala编译器明确知道此特征,因此初始化代码不是从类初始化程序运行,而是从其他方法运行。请注意特质唯一方法的for名称引用:
    trait Helper extends DelayedInit {
    def delayedInit(body: => Unit) = {
    println("dummy text, printed before initialization of C")
    body
    }
    }

    在实现 DelayedInit时,Scala编译器将其实现类或对象的所有初始化代码包装到for name函数中,然后将其传递给 delayedInit方法。没有初始化代码直接执行。这样,您还可以在运行初始化程序之前运行代码,例如,这允许Scala将应用程序的运行时指标打印到控制台,该控制台包裹在程序的入口和导出周围。但是,由于存在 some caveats of this approach,因此不建议使用 DelayedInit。您实际上应该仅依靠 App特性来解决 Application特性带来的问题。您不应该直接实现 DelayedInit

    如果需要,您仍然可以定义 main方法,只要在 object中定义它即可。这主要是风格问题:
    object HelloWorld {
    def main(args: Array[String]) {
    println("Hello, world!")
    }
    }

    关于scala - 在斯卡拉;我应该使用App特性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24437423/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com