Java notes: Run-time

Source: Internet
Author: User

First, the process

Abstract class process is used to encapsulate a process, that is, a program executed. Process is used primarily as a superclass of the object type created by the runtime Exec method or the object type created by the Processbuilder start method.

Second, run-time environment

Runtime encapsulates the run-time environment and cannot instantiate a runtime object, but instead obtains a reference to the current runtime object by calling the static method Runtime.getruntime method. Once you have a reference to the current runtime object, you can invoke some control over the state and behavior of the Java Virtual machine.

Third, memory management

Although Java provides automatic garbage collection, it is sometimes necessary to understand the size of the object heap or the size of the remaining space, and you can use the TotalMemory method and the Freememory method. We do not recycle objects that are no longer used until the Java garbage collector runs periodically. However, there are times when you want to reclaim obsolete objects before the next time the garbage collector is specified to run. You can call the GC method to require the garbage collector to run.

class Solution {    publicstaticvoid  main (string[] args) {        =  Runtime.getruntime ();         New double[100000];          for (int i = 0; i < arr.length; i++)             = 0.0;         NULL ;        System.out.println (Runtime.freememory ());        Runtime.gc ();        System.out.println (Runtime.freememory ());}    }
View Code


Iv. implementation of other procedures

In a secure environment, you can use Java to perform other heavyweight processes under a multitasking operating system. There are several forms of the Exec method that run a named program that you want to run, and allow for input parameters. The Exec method returns the Process object, which you can then use to control how the Java program interacts with the newly-run process. Because Java programs can run on various platforms and operating systems, the exec environment is independent.

Importjava.io.IOException;classSolution { Public Static voidMain (string[] args) {Runtime runtime=Runtime.getruntime (); Process Process=NULL; Try{Process= Runtime.exec ("notepad");//Run NotepadProcess.waitfor ();//wait for process to close}Catch(IOException exc) {System.out.println ("Cannot execute Notepad"); } Catch(Interruptedexception exc) {System.out.println ("Process interrupted");    } System.out.println (Process.exitvalue ()); }}
View Code


V. Process Creation Class

Import java.io.IOException; class Solution {    publicstaticvoid  main (string[] args) {        try  {            new processbuilder ("notepad.exe", "file.txt");            Builder.start ();         Catch (IOException exc) {            System.out.println ("Cannot execute Notepad");     }}}
View Code


Vi. System Class

The Currenttimemillis method allows you to get the number of milliseconds from midnight January 1, 1970 to the current time.

classSolution { Public Static voidMain (string[] args) {LongBegin =System.currenttimemillis (); Try {             for(inti = 0; I < 10; i++) {Thread.Sleep (1000); LongEnd =System.currenttimemillis (); System.out.println (End-begin); }        } Catch(Interruptedexception exc) {System.out.println ("Thread interrupted"); }    }}
View Code

Copies the array faster than the normal loop.

classSolution {Static int[] Copyarray (int[] arr) {        int[] cpy =New int[Arr.length]; System.arraycopy (arr,0, cpy, 0, arr.length); returncpy; }     Public Static voidMain (string[] args) {int[] arr = {1, 2, 3, 4, 5}; int[] cpy =Copyarray (arr);  for(inti:cpy)    System.out.println (i); }}
View Code

Environment properties.

class Solution {    publicstaticvoid  main (string[] args) {        System.out.println (System.getproperties ());}    }
View Code

Java notes: Runtime

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.