1
0

[LOGGING-135] Use double-checked locking idiom to improve thread-safety of Log4JLogger and LogKitLogger.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/logging/trunk@1448119 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart
2013-02-20 12:28:04 +00:00
parent 0c12cec132
commit ee85b25486
2 changed files with 33 additions and 23 deletions

View File

@@ -53,7 +53,7 @@ public class Log4JLogger implements Log, Serializable {
private static final String FQCN = Log4JLogger.class.getName();
/** Log to this logger */
private transient Logger logger = null;
private transient volatile Logger logger = null;
/** Logger name */
private final String name;
@@ -254,10 +254,16 @@ public class Log4JLogger implements Log, Serializable {
* Return the native Logger instance we are using.
*/
public Logger getLogger() {
if (logger == null) {
logger = Logger.getLogger(name);
Logger result = logger;
if (result == null) {
synchronized(this) {
result = logger;
if (result == null) {
logger = result = Logger.getLogger(name);
}
return this.logger;
}
}
return result;
}
/**

View File

@@ -41,7 +41,7 @@ public class LogKitLogger implements Log, Serializable {
// ------------------------------------------------------------- Attributes
/** Logging goes to this <code>LogKit</code> logger */
protected transient Logger logger = null;
protected transient volatile Logger logger = null;
/** Name of this logger */
protected String name = null;
@@ -65,12 +65,16 @@ public class LogKitLogger implements Log, Serializable {
* Return the underlying Logger we are using.
*/
public Logger getLogger() {
if (logger == null) {
logger = Hierarchy.getDefaultHierarchy().getLoggerFor(name);
Logger result = logger;
if (result == null) {
synchronized(this) {
result = logger;
if (result == null) {
logger = result = Hierarchy.getDefaultHierarchy().getLoggerFor(name);
}
return logger;
}
}
return result;
}
// ----------------------------------------------------- Log Implementation