Actually, in this case saving the result of System.currentTimeMillis() directly to the volatile field is fine.
I just have the habit of doing all the work on a temp variable before writing to a volatile field. Or read from a volatile field into a temp variable and then do all the work on the temp variable. In this case you rightly point out that there is no difference. Sent from my iPhone > On 2014/05/19, at 13:31, Matt Sicker <[email protected]> wrote: > > Was there any particular reason that the previous version saved the result of > System.currentTimeMillis() before assigning it? Is this a trick for volatile > variables or something? > > ---------- Forwarded message ---------- > From: <[email protected]> > Date: 18 May 2014 23:27 > Subject: svn commit: r1595738 - > /logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/util/CoarseCachedClock.java > To: [email protected] > > > Author: mattsicker > Date: Mon May 19 04:27:40 2014 > New Revision: 1595738 > > URL: http://svn.apache.org/r1595738 > Log: > Revert change to volatile long. > > Modified: > > logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/util/CoarseCachedClock.java > > Modified: > logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/util/CoarseCachedClock.java > URL: > http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/util/CoarseCachedClock.java?rev=1595738&r1=1595737&r2=1595738&view=diff > ============================================================================== > --- > logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/util/CoarseCachedClock.java > (original) > +++ > logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/util/CoarseCachedClock.java > Mon May 19 04:27:40 2014 > @@ -16,7 +16,6 @@ > */ > package org.apache.logging.log4j.core.util; > > -import java.util.concurrent.atomic.AtomicLong; > import java.util.concurrent.locks.LockSupport; > > /** > @@ -25,13 +24,14 @@ import java.util.concurrent.locks.LockSu > */ > public final class CoarseCachedClock implements Clock { > private static final CoarseCachedClock instance = new > CoarseCachedClock(); > - private final AtomicLong millis = new > AtomicLong(System.currentTimeMillis()); > + // ignore IDE complaints; volatile long is fine > + private volatile long millis = System.currentTimeMillis(); > > private final Thread updater = new Thread("Clock Updater Thread") { > @Override > public void run() { > while (true) { > - millis.set(System.currentTimeMillis()); > + millis = System.currentTimeMillis(); > > // avoid explicit dependency on sun.misc.Util > LockSupport.parkNanos(1000 * 1000); > @@ -62,6 +62,6 @@ public final class CoarseCachedClock imp > */ > @Override > public long currentTimeMillis() { > - return millis.get(); > + return millis; > } > } > > > > > > -- > Matt Sicker <[email protected]>
