From 47319d3cfca84c9fcd5eebea8d401c5ddc00113d Mon Sep 17 00:00:00 2001 From: Greg Gibeling Date: Tue, 25 Nov 2014 09:28:10 -0800 Subject: [PATCH] Lazily check for root, avoids unnecessary errors & resources Motivation: io.netty.util.internal.PlatformDependent.isRoot() depends on the IS_ROOT field which is filled in during class initialization. This spawns processes and consumes resources, which are not generally necessary to the complete functioning of that class. Modifications: This switches the class to use lazy initialization this field inside of the isRoot() method using double-checked locking (http://en.wikipedia.org/wiki/Double-checked_locking). Result: The first call to isRoot() will be slightly slower, at a tradeoff that class loading is faster, uses fewer resources and platform errors are avoided unless necessary. --- .../java/io/netty/util/internal/PlatformDependent.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/io/netty/util/internal/PlatformDependent.java b/common/src/main/java/io/netty/util/internal/PlatformDependent.java index d86eb49b02..530f13bf05 100644 --- a/common/src/main/java/io/netty/util/internal/PlatformDependent.java +++ b/common/src/main/java/io/netty/util/internal/PlatformDependent.java @@ -60,7 +60,7 @@ public final class PlatformDependent { private static final boolean IS_ANDROID = isAndroid0(); private static final boolean IS_WINDOWS = isWindows0(); - private static final boolean IS_ROOT = isRoot0(); + private static volatile Boolean IS_ROOT; private static final int JAVA_VERSION = javaVersion0(); @@ -114,6 +114,13 @@ public final class PlatformDependent { * {@code false} if on Windows. */ public static boolean isRoot() { + if (IS_ROOT == null) { + synchronized (PlatformDependent.class) { + if (IS_ROOT == null) { + IS_ROOT = isRoot0(); + } + } + } return IS_ROOT; }