Buggy code:
public class OStoragePerformanceStatistic implements OStoragePerformanceStatisticMXBean { private final ScheduledExecutorService updateTimer ; private volatile ScheduledFuture < ? > updateTask ; private final long snapshotInterval ; private static final int MEGABYTE = 1024 * 1024 ; private static final int NANOS_IN_SECOND = 1000000000 ; private final ThreadLocal < Deque < Long > > tlTimeStumps = new ThreadLocal < Deque < Long > > ( ) { @ Override protected Deque < Long > initialValue ( ) { } } ; private final AtomicReference < OStoragePerformanceStatistic . PerformanceCounters > performanceCounters = new AtomicReference < OStoragePerformanceStatistic . PerformanceCounters > ( ) ; private volatile int cacheHitsSnapshot = - 1 ; private volatile long readSpeedFromFileSnapshot = - 1 ; private volatile long readSpeedFromCacheSnapshot = - 1 ; private volatile long writeSpeedInCacheSnapshot = - 1 ; private volatile long writeSpeedInFileSnapshot = - 1 ; private volatile long commitTimeAvgSnapshot = - 1 ; private final int pageSize ; private final String mbeanName ; private final AtomicBoolean mbeanIsRegistered = new AtomicBoolean ( ) ; private final OStoragePerformanceStatistic . NanoTimer nanoTimer ; public OStoragePerformanceStatistic ( int pageSize , String storageName , long storageId ) { } public OStoragePerformanceStatistic ( int pageSize , String storageName , long storageId , OStoragePerformanceStatistic . NanoTimer nanoTimer , long snapshotInterval ) { } @ Override public void startMeasurement ( ) { } @ Override public void stopMeasurement ( ) { } @ Override public boolean isMeasurementEnabled ( ) { } public void registerMBean ( ) { } public void unregisterMBean ( ) { } @ Override public long getReadSpeedFromCacheInMB ( ) { } @ Override public long getReadSpeedFromCacheInPages ( ) { } @ Override public long getReadSpeedFromFileInPages ( ) { } @ Override public long getReadSpeedFromFileInMB ( ) { } @ Override public long getWriteSpeedInCacheInPages ( ) { } @ Override public long getWriteSpeedInCacheInMB ( ) { } @ Override public long getWriteSpeedInFileInPages ( ) { } @ Override public long getWriteSpeedInFileInMB ( ) { } @ Override public long getCommitTimeAvg ( ) { } @ Override public int getCacheHits ( ) { } public void startPageReadFromFileTimer ( ) { } private void pushTimeStamp ( ) { } public void stopPageReadFromFileTimer ( int readPages ) { } public void startPageReadFromCacheTimer ( ) { } public void stopPageReadFromCacheTimer ( ) { } public void startPageWriteToCacheTimer ( ) { } public void stopPageWriteToCacheTimer ( ) { final Deque < Long > stamps = tlTimeStumps . get ( ) ; if ( ( stamps . size ( ) ) > 0 ) { final long endTs = nanoTimer . getNano ( ) ; final long timeDiff = endTs - ( stamps . pop ( ) ) ; final OStoragePerformanceStatistic . PerformanceCounters pf = performanceCounters . get ( ) ; if ( pf != null ) { final AtomicReference < OTimeCounter > writeSpeedInCache = pf . writeSpeedInCache ; OTimeCounter oldWriteSpeedInCache = writeSpeedInCache . get ( ) ; OTimeCounter newWriteSpeedInCache = new OTimeCounter ( ( ( oldWriteSpeedInCache . getTime ( ) ) + timeDiff ) , ( ( oldWriteSpeedInCache . getCounter ( ) ) + 1 ) ) ; <START_BUG> while ( writeSpeedInCache . compareAndSet ( oldWriteSpeedInCache , newWriteSpeedInCache ) ) { <END_BUG> oldWriteSpeedInCache = writeSpeedInCache . get ( ) ; newWriteSpeedInCache = new OTimeCounter ( ( ( oldWriteSpeedInCache . getTime ( ) ) + timeDiff ) , ( ( oldWriteSpeedInCache . getCounter ( ) ) + 1 ) ) ; } } } } public void startPageWriteToFileTimer ( ) { } public void stopPageWriteToFileTimer ( ) { } public void incrementPageAccessOnCacheLevel ( boolean cacheHit ) { } public void startCommitTimer ( ) { } public void stopCommitTimer ( ) { } public interface NanoTimer { long getNano ( ) { } } private final class SnapshotTask implements Runnable { @ Override public void run ( ) { } } private static final class SnapshotTaskFactory implements ThreadFactory { private final String storageName ; public SnapshotTaskFactory ( String storageName ) { } @ Override public Thread newThread ( Runnable r ) { } } private static final class PerformanceCounters { private final AtomicReference < OTimeCounter > cacheHits = new AtomicReference < OTimeCounter > ( new OTimeCounter ( 0 , 0 ) ) ; private final AtomicReference < OTimeCounter > readSpeedFromFile = new AtomicReference < OTimeCounter > ( new OTimeCounter ( 0 , 0 ) ) ; private final AtomicReference < OTimeCounter > readSpeedFromCache = new AtomicReference < OTimeCounter > ( new OTimeCounter ( 0 , 0 ) ) ; private final AtomicReference < OTimeCounter > writeSpeedInCache = new AtomicReference < OTimeCounter > ( new OTimeCounter ( 0 , 0 ) ) ; private final AtomicReference < OTimeCounter > writeSpeedInFile = new AtomicReference < OTimeCounter > ( new OTimeCounter ( 0 , 0 ) ) ; private final AtomicReference < OTimeCounter > commitTimeAvg = new AtomicReference < OTimeCounter > ( new OTimeCounter ( 0 , 0 ) ) ; } }

Fix:
while ( ! ( writeSpeedInCache . compareAndSet ( oldWriteSpeedInCache , newWriteSpeedInCache ) ) ) {