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

Igor Ignatyev iignatyev at openjdk.java.net
Wed Oct 7 18:12:20 UTC 2020


On Wed, 7 Oct 2020 06:30:43 GMT, Aleksey Shipilev <shade at openjdk.org> wrote:

>> 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.

@RogerRiggs , as Aleksey pointed out `ProcessHandle::toString` isn't specified to return the string which contains only
`pid` (in fact it's not specified at all), so I don't think we should rely on the current implementation.

although it doesn't matter much, I've removed explicit conversion here and replaced `String::valueOf` with
`Long::toString` in other places.

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

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



More information about the build-dev mailing list