Java/fr: Difference between revisions

From Alliance Doc
Jump to navigation Jump to search
No edit summary
(Created page with "Les logiciels Java sont fréquemment distribués sous forme de fichiers JAR portant le suffixe <tt>jar</tt>. Pour utiliser un logiciel Java, utilisez la commande {{Commande|j...")
Line 6: Line 6:
* <tt>javac</tt> pour appeler le compilateur Java qui convertit un fichier source Java en bytecode.  
* <tt>javac</tt> pour appeler le compilateur Java qui convertit un fichier source Java en bytecode.  


Java software is frequently distributed in the form of a JAR file with the extension <tt>jar</tt>. You can use such software by means of the following command,
Les logiciels Java sont fréquemment distribués sous forme de fichiers JAR portant le suffixe <tt>jar</tt>. Pour utiliser un logiciel Java, utilisez la commande
{{Command|java -jar file.jar}}
{{Commande|java -jar file.jar}}   
assuming the JAR file has been compiled to operate as an autonomous program (i.e. it possesses a <tt>Main-class</tt> manifest header).    


==Parallelism in Java==
==Parallelism in Java==

Revision as of 16:44, 25 April 2017

Other languages:

Java est un langage de programmation de haut niveau orienté objet créé en 1995 par Sun Microsystems (rachetée en 2009 par Oracle). L'objectif central de Java est que les logiciels écrits dans ce langage obéissent au principe write once, run anywhere et sont très facilement portables sur plusieurs systèmes d’exploitation par le fait que le code source Java se compile en code octal (bytecode) pouvant être exécuté sur un environnement Java (JVM pourJava virtual machine); différentes architectures et plateformes peuvent donc constituer un environnement uniforme. Cette caractéristique fait de Java un langage populaire dans certains contextes et notamment pour l'apprentissage de la programmation. Même si l'accent n'est pas sur la performance, il existe des moyens d'augmenter la vitesse d'exécution et le langage a connu une certaine popularité auprès des scientifiques dans des domaines comme les sciences de la vie, d'où sont issus par exemple les outils d'analyse génomique GATK du Broad Institute. Le but de cette page n'est pas d'enseigner le langage Java, mais de fournir des conseils et suggestions pour son utilisation dans l'environnement CHP de Calcul Canada.

Calcul Canada met à la disposition des utilisateurs plusieurs environnements Java via la commande module. En principe, vous aurez un seul module Java chargé à la fois. Les principales commandes associées aux modules Java sont :

* java pour lancer en environnement Java;
  • javac pour appeler le compilateur Java qui convertit un fichier source Java en bytecode.

Les logiciels Java sont fréquemment distribués sous forme de fichiers JAR portant le suffixe jar. Pour utiliser un logiciel Java, utilisez la commande

Question.png
[nom@serveur ~]$ java -jar file.jar

Parallelism in Java

Threading

Java includes built-in support for threading, obviating the need for separate interfaces and libraries like OpenMP, pthreads and Boost threads used in other languages. The principal Java object for handling concurrency is the Thread class which a programmer can use by either providing a Runnable method to the standard Thread class or by subclassing the Thread class. As an example of this second approach, consider the following toy program:

File : thread.java

public class HelloWorld extends Thread {
        public void run() {
            System.out.println("Hello World!");
        }
        public static void main(String args[]) {
            (new HelloWorld()).start();
        }
}


This second approach is generally the simplest to use but suffers from the drawback that Java does not permit multiple inheritance, so the class which implements multithreading is unable to subclass any other - potentially more useful - class.

MPI and Java

One common method for using MPI-style parallelism in a Java program is the MPJ Express library.

Pitfalls

Memory Issues

Java uses an automatic system called garbage collection to identify variables which are out of scope and return the memory associated with them to the operating system which however doesn't stop many Java programs from requiring significant amounts of memory to run correctly. When a Java virtual machine is launched using the java command by default the initial and maximum heap size are set to 1/64 and 1/4 of the system's physical memory respectively. This amount, particularly the maximum heap size, may well be inadequate and leaves a substantial amount of physical memory unused. To correct this problem, you can tell the Java virtual machine the maximum amount of memory to use with the command line argument Xmx, for instance

Question.png
[name@server ~]$ java -Xmx8192m -jar file.jar

tells the Java virtual machine that it can use up to 8192 MB (8 GB) of memory. You can set the initial heap size with the argument Xms and you can see all the command line options the JVM is going to run with by specifying the following flag -XX:+PrintCommandLineFlags.

Alternatively, you can use the _JAVA_OPTIONS environment variable to set the run-time options rather that passing them on the command line. This is especially convenient if you launch multiple Java calls, or call a Java program from another Java program. Here is an example how to do it:

Question.png
[name@server ~]$ export _JAVA_OPTIONS="-Xms256m -Xmx2g"

When your Java program is run, it will produce a diagnostic message like this one "Picked up _JAVA_OPTIONS", verifying that the options have been picked up.

Please remember that the Java virtual machine itself creates a memory usage overhead. We recommend specifying the memory limit for your job as 1-2GB more than your setting on the Java command line option -Xmx.

Garbage Collection

By default, the Java VM uses a parallel garbage collector (GC) and sets a number of GC threads equal to the number of CPU cores on a given node, whether a Java job is threaded or not. Each GC thread consumes memory. Moreover, the amount of memory each GC thread consumes is proportional to the amount of physical memory. Therefore, we highly recommend matching the number of GC threads to the number of CPU cores you requested from the scheduler in your job submission script, like so -XX:ParallelGCThreads=12 for example. You can also use the serial garbage collector by specifying the following option -XX:+UseSerialGC, whether your job is parallel or not.

The volatile Keyword

This keyword has a sense very different from that which C/C++ programmers are accustomed to. In Java volatile when applied to a variable has the effect of ensuring that its value is always read from and written to main memory, which can help to ensure that modifications of this variable are made visible to other threads. That said, there are contexts in which the use of the volatile keyword are not sufficient to avoid race conditions and the synchronized keyword is required to ensure program consistency.

Further Reading

Scott Oaks and Henry Wong, Java Threads: Understanding and Mastering Concurrent Programming (3rd edition) (O'Reilly, 2012)