Runtime.currentRuntime
Doug Lea
dl at cs.oswego.edu
Sun Nov 29 12:58:38 UTC 2015
On 11/28/2015 10:07 AM, Alan Bateman wrote:
> On 28/11/2015 11:37, Doug Lea wrote:
>>
>> Does anyone know why the static singleton in java.lang.Runtime is
>> not declared as final?
>>
>> public class Runtime {
>> private static Runtime currentRuntime = new Runtime();
>>
>> I don't see a reason, so it seems likely that somehow no one
>> has noticed before that this should be final (for thread safety and
>> optimizability). But it would be nice if someone with access to
>> full JCK etc tests made sure that adding "final" didn't break
>> anything.
> I don't see a reason either. I checked the pre-OpenJDK history and it's been
> like this since 1995. If it's deliberately non-final then I would expect a comment.
>
> I just built and ran the tests with it final and don't see any issues.
>
Since you must have all this handy, could you open and close CR? :-)
Thanks! David Holmes and I have already reviewed.
On 11/28/2015 11:02 PM, David Holmes wrote:
>
> Given when this gets initialized thread-safety is not an issue. Seems unlikely to play much of a role in optimization either.
>
It would noticeably matter if the class had methods that were called more
often. (Say, for example, spin-loop support :-)
> No reason within the JDK for it to not be final that I can see. Also no reason I can see for the class itself and/or all its methods to not be final.
I think you are right. Runtime cannot be subclassed outside of the JDK
(there are no accessible constructors), and no reason to ever subclass within.
In fact, there is no reason not to just make all the methods static,
as one of them (runFinalizersOnExit) inconsistently already is. But
these changes would require a CCC request that might be controversial
because javac and IDEs would then start complaining about existing code
calling static methods with instances. So, for now, just declaring the
instance final suffices.
-Doug
More information about the core-libs-dev
mailing list