Yes, but take a look at the Effective Java section. What you coded works 
correctly but it accesses “instance” twice. By reading instance into a temp 
variable that isn’t volatile it claims you can get a bit of a performance boost 
since the volatile variable is accessed only once.

Ralph

On Sep 14, 2014, at 10:10 AM, Matt Sicker <[email protected]> wrote:

> Because volatile works properly in Java 1.6? At least that's what I gathered 
> the last time I discussed this with Ralph.
> 
> On 14 September 2014 09:39, Gary Gregory <[email protected]> wrote:
> This looks like the double checked locking pattern but it does not use a temp 
> result var. Any reason why it is not like the 'classic' DCL (see Effective 
> Java item 71).
> 
> Gary
> 
> ---------- Forwarded message ----------
> From: <[email protected]>
> Date: Sun, Sep 14, 2014 at 3:00 AM
> Subject: git commit: Lazily initialize singleton instances spawning threads.
> To: [email protected]
> 
> 
> Repository: logging-log4j2
> Updated Branches:
>   refs/heads/master b257c78af -> 2ce32db31
> 
> 
> Lazily initialize singleton instances spawning threads.
> 
>   - See discussion in LOG4J2-819.
> 
> 
> Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
> Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/2ce32db3
> Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/2ce32db3
> Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/2ce32db3
> 
> Branch: refs/heads/master
> Commit: 2ce32db31a53a07914cf620df25e4c6a7e4fbdba
> Parents: b257c78
> Author: Matt Sicker <[email protected]>
> Authored: Sun Sep 14 02:00:12 2014 -0500
> Committer: Matt Sicker <[email protected]>
> Committed: Sun Sep 14 02:00:12 2014 -0500
> 
> ----------------------------------------------------------------------
>  .../org/apache/logging/log4j/core/util/CachedClock.java  | 11 ++++++++++-
>  .../logging/log4j/core/util/CoarseCachedClock.java       | 11 ++++++++++-
>  2 files changed, 20 insertions(+), 2 deletions(-)
> ----------------------------------------------------------------------
> 
> 
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/2ce32db3/log4j-core/src/main/java/org/apache/logging/log4j/core/util/CachedClock.java
> ----------------------------------------------------------------------
> diff --git 
> a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/CachedClock.java
>  
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/CachedClock.java
> index c4324c7..6dfb34c 100644
> --- 
> a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/CachedClock.java
> +++ 
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/CachedClock.java
> @@ -28,7 +28,8 @@ import java.util.concurrent.locks.LockSupport;
>   */
>  public final class CachedClock implements Clock {
>      private static final int UPDATE_THRESHOLD = 0x3FF;
> -    private static final CachedClock instance = new CachedClock();
> +    private static volatile CachedClock instance;
> +    private static final Object INSTANCE_LOCK = new Object();
>      private volatile long millis = System.currentTimeMillis();
>      private volatile short count = 0;
> 
> @@ -50,6 +51,14 @@ public final class CachedClock implements Clock {
>      }
> 
>      public static CachedClock instance() {
> +        // LOG4J2-819: use lazy initialization of threads
> +        if (instance == null) {
> +            synchronized (INSTANCE_LOCK) {
> +                if (instance == null) {
> +                    instance = new CachedClock();
> +                }
> +            }
> +        }
>          return instance;
>      }
> 
> 
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/2ce32db3/log4j-core/src/main/java/org/apache/logging/log4j/core/util/CoarseCachedClock.java
> ----------------------------------------------------------------------
> diff --git 
> a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/CoarseCachedClock.java
>  
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/CoarseCachedClock.java
> index 77ffa4d..9fa7b15 100644
> --- 
> a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/CoarseCachedClock.java
> +++ 
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/CoarseCachedClock.java
> @@ -23,7 +23,8 @@ import java.util.concurrent.locks.LockSupport;
>   * the cost of some accuracy.
>   */
>  public final class CoarseCachedClock implements Clock {
> -    private static final CoarseCachedClock instance = new 
> CoarseCachedClock();
> +    private static volatile CoarseCachedClock instance;
> +    private static final Object INSTANCE_LOCK = new Object();
>      // ignore IDE complaints; volatile long is fine
>      private volatile long millis = System.currentTimeMillis();
> 
> @@ -50,6 +51,14 @@ public final class CoarseCachedClock implements Clock {
>       * @return the singleton instance
>       */
>      public static CoarseCachedClock instance() {
> +        // LOG4J2-819: use lazy initialization of threads
> +        if (instance == null) {
> +            synchronized (INSTANCE_LOCK) {
> +                if (instance == null) {
> +                    instance = new CoarseCachedClock();
> +                }
> +            }
> +        }
>          return instance;
>      }
> 
> 
> 
> 
> 
> -- 
> E-Mail: [email protected] | [email protected] 
> Java Persistence with Hibernate, Second Edition
> JUnit in Action, Second Edition
> Spring Batch in Action
> Blog: http://garygregory.wordpress.com 
> Home: http://garygregory.com/
> Tweet! http://twitter.com/GaryGregory
> 
> 
> 
> -- 
> Matt Sicker <[email protected]>

Reply via email to