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:
Specify the desired VM argument during startup as follows:
java -Dstack.size=1024k ...
Within your Java code, retrieve this argument using the System.getProperty() method:
String stackSize = System.getProperty("stack.size");
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!