java.lang.System

來源:互聯網
上載者:User

標籤:stream   explicit   之間   交換   his   generate   影響   exit   code   

System類建構函式由private修飾,不可以被執行個體化,載入時調用static代碼塊。

System類提供了標準輸入輸出資料流,錯誤輸出資料流;其中out和err的類型是PrintStream

/** * The <code>System</code> class contains several useful class fields * and methods. It cannot be instantiated. * * <p>Among the facilities provided by the <code>System</code> class * are standard input, standard output, and error output streams; * access to externally defined properties and environment * variables; a means of loading files and libraries; and a utility * method for quickly copying a portion of an array. * * @author  unascribed * @since   JDK1.0 */public final class System {    /* register the natives via the static initializer.     *     * VM will invoke the initializeSystemClass method to complete     * the initialization for this class separated from clinit.     * Note that to use properties set by the VM, see the constraints     * described in the initializeSystemClass method.     */    private static native void registerNatives();    static {        registerNatives();    }    /** Don‘t let anyone instantiate this class */    private System() {    }

System.exit(int),終止當前虛擬機器的運行,什麼叫做當前虛擬機器呢?

public static void exit(int status) {        Runtime.getRuntime().exit(status);    }

如果在某個線程中調用了exit會怎樣?下面的例子輸出結果是hello,把子線程和主線程的內容交換也是一樣的結果。就是說不管在哪個線程調用了exit,整個應用都退出了。

package com.ysdx.test;public class JavaTest {    public static void main(String[] args) {        Thread thread = new Thread(() -> {            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            System.out.println("hello!");            System.exit(1);        });        thread.start();        try {            Thread.sleep(3000);        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        System.out.println("Hello from main thread!");    }}

 兩個虛擬機器之間是互不影響的

其中JavaTest1如下

public class JavaTest1 {    public static void main(String[] args) {                try {            Thread.sleep(3000);        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        System.out.println("Hello from main thread!");    }}

---------------------

System中gc相關操作

/**     * Runs the garbage collector.     * <p>     * Calling the <code>gc</code> method suggests that the Java Virtual     * Machine expend effort toward recycling unused objects in order to     * make the memory they currently occupy available for quick reuse.     * When control returns from the method call, the Java Virtual     * Machine has made a best effort to reclaim space from all discarded     * objects.     * <p>     * The call <code>System.gc()</code> is effectively equivalent to the     * call:     * <blockquote><pre>     * Runtime.getRuntime().gc()     * </pre></blockquote>     *     * @see     java.lang.Runtime#gc()     */    public static void gc() {        Runtime.getRuntime().gc();    }    /**     * Runs the finalization methods of any objects pending finalization.     * <p>     * Calling this method suggests that the Java Virtual Machine expend     * effort toward running the <code>finalize</code> methods of objects     * that have been found to be discarded but whose <code>finalize</code>     * methods have not yet been run. When control returns from the     * method call, the Java Virtual Machine has made a best effort to     * complete all outstanding finalizations.     * <p>     * The call <code>System.runFinalization()</code> is effectively     * equivalent to the call:     * <blockquote><pre>     * Runtime.getRuntime().runFinalization()     * </pre></blockquote>     *     * @see     java.lang.Runtime#runFinalization()     */    public static void runFinalization() {        Runtime.getRuntime().runFinalization();    }

 順便看一下Runtime,

即便不顯示調用gc,JVM也會自動在一個單獨的線程中執行recycling process;

即便不顯示調用runFinalization,JVM也會自動在一個單獨的線程中執行finalization process;

可以很明顯的看出gc和runFinalization是不同的,一個叫做回收過程,一個叫做終結過程。

調用runFinalization後,終結過程會調用已經被廢棄但是尚未調用其finalization方法的對象。

函數注釋裡的描述都使用了suggest和has made its best effort,可能這兩個函數還有不足之處。

/**     * Runs the garbage collector.     * Calling this method suggests that the Java virtual machine expend     * effort toward recycling unused objects in order to make the memory     * they currently occupy available for quick reuse. When control     * returns from the method call, the virtual machine has made     * its best effort to recycle all discarded objects.     * <p>     * The name <code>gc</code> stands for "garbage     * collector". The virtual machine performs this recycling     * process automatically as needed, in a separate thread, even if the     * <code>gc</code> method is not invoked explicitly.     * <p>     * The method {@link System#gc()} is the conventional and convenient     * means of invoking this method.     */    public native void gc();    /* Wormhole for calling java.lang.ref.Finalizer.runFinalization */    private static native void runFinalization0();    /**     * Runs the finalization methods of any objects pending finalization.     * Calling this method suggests that the Java virtual machine expend     * effort toward running the <code>finalize</code> methods of objects     * that have been found to be discarded but whose <code>finalize</code>     * methods have not yet been run. When control returns from the     * method call, the virtual machine has made a best effort to     * complete all outstanding finalizations.     * <p>     * The virtual machine performs the finalization process     * automatically as needed, in a separate thread, if the     * <code>runFinalization</code> method is not invoked explicitly.     * <p>     * The method {@link System#runFinalization()} is the conventional     * and convenient means of invoking this method.     *     * @see     java.lang.Object#finalize()     */    public void runFinalization() {        runFinalization0();    }

 

java.lang.System

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.