Java Runtime traceMethodCalls() 方法



描述

Java Runtime traceMethodCalls(boolean on) 方法啟用/停用方法呼叫的跟蹤。如果布林型引數為 true,則此方法表示 Java 虛擬機器在其呼叫每個方法時發出該方法的除錯資訊。此資訊的格式以及將其發出的檔案或其他輸出流取決於主機環境。如果虛擬機器不支援此功能,則它可能會忽略此請求。使用引數 false 呼叫此方法表示虛擬機器停止發出每個呼叫的除錯資訊。

宣告

以下是java.lang.Runtime.traceMethodCalls() 方法的宣告

public void traceMethodCalls(boolean on)

引數

  • on − true 表示啟用指令跟蹤;false 表示停用此功能。

返回值

此方法不返回值。

異常

示例:啟用方法呼叫跟蹤

以下示例演示了 Java Runtime traceMethodCalls() 方法的用法。我們使用 traceMethodCalls(true) 方法啟用了當前 JVM 的方法呼叫跟蹤。

package com.tutorialspoint;

public class RuntimeDemo {

   public static void main(String[] args) {

      // print the state of the program
      System.out.println("Program is starting...");

      // start tracing for instructions
      System.out.println("Enabling tracing...");
      Runtime.getRuntime().traceMethodCalls(true);
      System.out.println("Done!");
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Program is starting...
Enabling tracing...
Done!

示例:停用方法呼叫跟蹤

以下示例演示了 Java Runtime traceMethodCalls() 方法的用法。我們使用 traceMethodCalls(false) 方法停用了當前 JVM 的方法呼叫跟蹤。

package com.tutorialspoint;

public class RuntimeDemo {

   public static void main(String[] args) {

      // print the state of the program
      System.out.println("Program is starting...");

      // start tracing for instructions
      System.out.println("Disabling tracing...");
      Runtime.getRuntime().traceMethodCalls(false);
      System.out.println("Done!");
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Program is starting...
Disabling tracing...
Done!
java_lang_runtime.htm
廣告