RFR: 8254102: use ProcessHandle::pid instead of ManagementFactory::getRuntimeMXBean to get pid in tests

Aleksey Shipilev shade at openjdk.java.net
Wed Oct 7 06:40:11 UTC 2020


On Wed, 7 Oct 2020 01:47:23 GMT, Roger Riggs <rriggs at openjdk.org> wrote:

>> Hi all,
>> 
>> could you please review this small cleanup which replaces
>> `ManagementFactory.getRuntimeMXBean().getName().split("@")[0]` w/ `ProcessHandle.current().pid()` to get current
>> process pid?  Thanks,
>> -- Igor
>
> test/failure_handler/test/sanity/Suicide.java line 36:
> 
>> 34:             String osName = System.getProperty("os.name");
>> 35:             if (osName.contains("Windows")) {
>> 36:                 cmd = "taskkill.exe /F /PID " + pidStr;
> 
> This can be simplified to ProcessHandle.current().toString().  It returns the pid of the process as a string.
> 
> Explicitly converting it to a string is not necessary.  The "+" concatenation would convert the number to a string.

Yes, can just have `long pid` in this case.

I don't see that `ProcessHandle.toString` is *specified* to return the string with pid, so relying on that is brittle.
We might call `Long.toString` here directly, to avoid jumping through a few calls. But seeing how all this is a test
code, that does not seem necessary.

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

PR: https://git.openjdk.java.net/jdk/pull/534



More information about the build-dev mailing list