ホームページ  >  記事  >  Java  >  Javaランタイムクラス

Javaランタイムクラス

WBOY
WBOYオリジナル
2024-08-30 16:10:26499ブラウズ

Java のランタイム環境との対話に使用されるクラスは、Java ではランタイム クラスと呼ばれ、Runtime.getRuntime()、exit(int status)、addShutdownHook(Threadフック)、Process exec(などのいくつかのメソッドを提供します) String コマンド)は、宣言が public であるプロセスの実行、GC の呼び出し、総メモリの取得、空きメモリの取得などのために、入出力例外、availableProcessors()、freeMemory()、totalMemory() などをスローします。 class Runtime はオブジェクトを拡張し、Java の 1 つのアプリケーションだけが java.lang.Runtime クラスのインスタンスを 1 つだけ使用でき、Runtime クラスのシングルトン インスタンスは Java の Runtime.getRuntime() メソッドを使用して返されます。

Java ランタイム クラスのメソッド

Java Runtime Classにはいくつかのメソッドがあります。それらは次のとおりです:

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

1.ランタイム getRuntime()

ランタイム クラスのインスタンスは、ランタイム getRuntime() メソッドを使用して返されます。 Java ランタイム クラスのランタイム getRuntime() メソッドを示すために、以下の Java プログラムを考えてみましょう。

コード:

//a class called program is defined
public class program
{
//main method is called
public static void main(String[] args)
{
// The current runtime in association with process is obtained using getRuntime method.
Runtime roll = Runtime.getRuntime();
// The current free memory for the current runtime is obtained using freeMemory() method.
System.out.println("The current free memory for the current runtime is" + roll.freeMemory());
}
}

出力:

Javaランタイムクラス

説明: 上記のプログラムでは、program というクラスが定義されています。次に、main メソッドが呼び出されます。次に、プロセスに関連する現在のランタイムが getRuntime メソッドを使用して取得されます。現在のランタイムの現在の空きメモリは、freeMemory() メソッドを使用して取得されます。

2. exit(int ステータス)

現在の仮想マシンは、Runtime exit(int status) メソッドを使用して終了されます。 Java ランタイム クラスの exit(int status) メソッドを示すために、以下の Java プログラムを考えてみましょう。

コード:

//a class called program is defined
public class program
{
//main method is called
public static void main(String[] args)
{
//the current runtime or program is caused to exit using exit(int status) method
Runtime.getRuntime().exit(0);
//This statement is not executed because the program is terminated by the usage of exit(int status) method above
System.out.println("Checking if the program executes this statement depsite the use of exit(int status) method");
}
}

出力:

Javaランタイムクラス

上記のプログラムの出力は空白です。

説明: 上記のプログラムでは、program というクラスが定義されています。次に、main メソッドが呼び出されます。次に、現在のランタイムまたはプログラムが exit(int status) メソッドを使用して終了します。上記のステートメントでは exit(int status) メソッドを使用することでプログラムが終了するため、ステートメントは実行されません。

3. addShutdownHook(スレッドフック)

addShutdownHook(Thread Hook)メソッドを使用して、新しいフックスレッドが登録されます。 Java ランタイム クラスの addShutdownHook(Thread Hook) メソッドを示すために、以下の Java プログラムを考えてみましょう。

コード:

//a class called program is defined
public class program
{
// when the program is about to exit, this class extending the thread is called
static class Mess extends Thread
{
public void run()
{
System.out.println("The program is going to exit");
}
}
//main method is called
public static void main(String[] args)
{
try
{
//the class mess is registered as shut down hook
Runtime.getRuntime().addShutdownHook(new Mess());
//The thread is made to sleep for certain seconds
System.out.println("Five seconds of waiting time for the program to exit");
Thread.sleep(5);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}

出力:

Javaランタイムクラス

説明: 上記のプログラムでは、program というクラスが定義されています。プログラムが終了しようとすると、スレッドを拡張するこのクラスが呼び出されます。次に、main メソッドが呼び出されます。次に、クラスの混乱がフックのシャットダウンとして登録されます。その後、スレッドは一定秒間スリープ状態になります。

4.プロセス exec(文字列コマンド) が IOException

をスローする

指定されたコマンドは、Process exec(String command) throws IOException メソッドを使用して別のプロセスで実行されます。 Process exec(String command) が Java ランタイム クラス

の IOException メソッドをスローすることを示すために、以下の Java プログラムを考えてみましょう。

コード:

//a class called program is defined
public class program
{
//main method is called
public static void main(String[] args)
{
try
{
// a process is created to execute google chrome
Process proc = Runtime.getRuntime().exec("google-chrome");
System.out.println("Successfully started google chrome");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

出力:

Javaランタイムクラス

説明: 上記のプログラムでは、program というクラスが定義されています。次に、main メソッドが呼び出されます。次に、Google Chrome を実行するプロセスが作成されます。

5. availableProcessors()

利用可能なプロセッサの数は、availableProcesors() メソッドを使用して返されます。 Java ランタイム クラスの availableProcessors() メソッドを示すために、以下の Java プログラムを考えてみましょう。

コード:

//a class called program is defined
public class program
{
//main method is called
public static void main(String[] args)
{
//checking for the number of available processors using availableprocessors() method.
System.out.println("The number of available processors are " + Runtime.getRuntime().availableProcessors());
}
}

出力:

Javaランタイムクラス

説明: 上記のプログラムでは、program というクラスが定義されています。次に、main メソッドが呼び出されます。次に、availableprocessors() メソッドを使用して、使用可能なプロセッサの数のチェックが行われます。

6. freeMemory()

空きメモリの量は、freeMemory() メソッドを使用して JVM に返されます。

Java ランタイム クラスの freeMemory() メソッドを示すために、以下の Java プログラムを考えてみましょう。

コード:

//a class called program is defined
public class program
{
//main method is called
public static void main(String[] args)
{
//The amount of free memory available is given by using freememory() method
System.out.println("The amount of free memory available is " + Runtime.getRuntime().freeMemory());
}
}

出力:

Javaランタイムクラス

Explanation: In the above program, a class called program is defined. Then the main method is called. Then the amount of free memory available is given by using freememory() method.

7. totalMemory()

The amount of total memory is returned in JVM using totalMemory()method. Consider the below Java program to demonstrate totalMemory() method of the Java Run time class.

Code:

//a class called program is defined
public class program
{
//main method is called
public static void main(String[] args)
{
//The amount of total memory available is given by using totalmemory() method
System.out.println("The amount of free memory available is " + Runtime.getRuntime().totalMemory());
}
}

Output:

Javaランタイムクラス

Explanation: In the above program, a class called program is defined. Then the main method is called. Then the amount of total memory available is given by using totalmemory() method.

以上がJavaランタイムクラスの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:Javaコンソール次の記事:Javaコンソール