alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (Application.scala)

This example Scala source code file (Application.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

app, app, application, application, array, array, long, long

The Scala Application.scala source code

/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2002-2010, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */
package scala

import scala.compat.Platform.currentTime

/** <p>
 *    The <code>Application trait can be used to quickly turn objects
 *    into executable programs, but is <em>not recommended.
 *    Here is an example:
 *  </p>
 *  <b>object Main extends Application {
 *    Console.println("Hello World!")
 *  }
 *  </pre>
 *  <p>
 *    Here, object <code>Main inherits the main method
 *    of <code>Application. The body of the Main object
 *    defines the main program. This technique does not work if the main
 *    program depends on command-line arguments (which are not accessible
 *    with the technique presented here).
 *  </p>
 *  <p>
 *    It is possible to time the execution of objects that inherit from class
 *    <code>Application by setting the global scala.time
 *    property. Here is an example for benchmarking object <code>Main:
 *  </p>
 *  java -Dscala.time Main
 *  </pre>
 *  <p>
 *    In practice the <code>Application trait has a number of serious
 *    pitfalls:
 *  </p>
 *  <ul>
 *    <li> Threaded code that references the object will block until static 
 *    initialization is complete.  However, because the entire execution of an 
 *    <code>object extending Application takes place during
 *    static initialization, concurrent code will <em>always deadlock if 
 *    it must synchronize with the enclosing object.</li>
 *    <li>As described above, there is no way to obtain the 
 *    command-line arguments because all code in body of an <code>object
 *    extending <code>Application is run as part of the static initialization
 *    which occurs before <code>Application's main method 
 *    even begins execution.</li>  
 *    <li>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 <code>object extending 
 *    <code>Application.  This can lead to a significant 
 *    performance degradation.</li>
 *  </ul>
 *
 *  It is recommended to use the `App` trait instead.
 *  <pre>
 *  <b>object Main {
 *    <b>def main(args: Array[String]) {
 *      //..
 *    }
 *  }
 *  </pre>
 *
 *  @author  Matthias Zenger
 *  @version 1.0, 10/09/2003
 */
@deprecated("use App instead", "2.9.0")
trait Application {

  /** The time when the execution of this program started, in milliseconds since 1
    * January 1970 UTC. */
  val executionStart: Long = currentTime

  /** The default main method.
   *
   *  @param args the arguments passed to the main method
   */
  def main(args: Array[String]) {
    if (util.Properties.propIsSet("scala.time")) {
      val total = currentTime - executionStart
      Console.println("[total " + total + "ms]")
    }
  }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala Application.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.