Home > Java > javaTutorial > How to Determine if a JVM Argument is Explicitly Set or Using its Default Value?

How to Determine if a JVM Argument is Explicitly Set or Using its Default Value?

Mary-Kate Olsen
Release: 2024-11-10 03:39:02
Original
396 people have browsed it

How to Determine if a JVM Argument is Explicitly Set or Using its Default Value?

Accessing VM Arguments within a Java Application

Problem:

How can you determine whether an option passed to the Java Virtual Machine (JVM) is explicitly set or has its default value? In this case, the objective is to create a thread with a higher native stack size compared to the default. However, if the user specifies the -Xss option, the default stack size should be applied.

Solution:

To retrieve information about VM arguments within your Java application, you can utilize the System.getProperty() method. This method allows you to access system properties, including those set as arguments when starting the JVM.

Here's how to implement this using -Dname=value:

  1. Specify the desired VM argument during startup as follows:

    java -Dstack.size=1024k ...
    Copy after login
  2. Within your Java code, retrieve this argument using the System.getProperty() method:

    String stackSize = System.getProperty("stack.size");
    Copy after login

You can then use the retrieved value of stackSize to determine whether the -Xss argument was explicitly set or not. If it is set, create threads with the specified stack size; otherwise, use the default stack size.

The above is the detailed content of How to Determine if a JVM Argument is Explicitly Set or Using its Default Value?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template