Code example for AtomicLong

Methods: get, set

0
    // first determine current sample rate 
    long timeStamp = SystemClock.elapsedRealtime();
    long timeInterval = timeStamp - lastTimeStamp.get();
     
    long sampleIncrement =
        currentRecordCount - ( lastRecordCount + availableSampleCount.get() );
     
    // now update internal fields 
    updateFields( currentRecordCount, timeStamp );
     
    // calculate the required wait time for a minimum of available samples 
    long waitTime = calculateNewWaitTime( sampleIncrement, timeInterval );
     
    // clear rate changed flag 
    sampleRateHasChanged = false;
     
    float sampleRate = sampleIncrement;
    sampleRate /= timeInterval;
     
    Logger.getInstance().debug( 
        this,
        "available sampels = " + availableSampleCount.get()
            + ", sample rate = " + ( sampleRate * 1000 )
            + "/s, current wait time = " + waitTime + " ms" );
     
    return waitTime;
  } 
   
  /** 
   * Method to calculate an updated time to wait for samples 
   *  
   * @param sampleIncrement 
   *          the current sample increment 
   * @param timeInterval 
   *          the current time interval the current error 
   * @return the new calculated time in milliseconds 
   */ 
  private final long calculateNewWaitTime( long sampleIncrement, long timeInterval )
  { 
    long currentWaitTime = 0L;
    long missingSampleCount = getMissingSampleCount();
     
    if ( sampleRateHasChanged )
    { 
      // after a sample rate change we need a new cycle for a better sample rate 
      // estimation 
      currentWaitTime = SAMPLE_RATE_DELAY;
 
      // clear missing sample count to avoid error correction in next turn 
      missingSampleCount = 0L;
    } 
    else 
    { 
      if ( missingSampleCount > 0L )
      { 
        // avoid division by zero and allow a growing wait time in case of missing increment 
        sampleIncrement = Math.max( sampleIncrement, 1 );
         
        // calculate new wait time and clip to range           
        long waitTime =
            (long) ( ( missingSampleCount * AF * timeInterval )
                / sampleIncrement );
        currentWaitTime =
            Math.max( Math.min( waitTime, MAX_FREQUENCY ), MIN_FREQUENCY );
      } 
    } 
     
    return currentWaitTime;
  } 
   
  /** 
   * Method to determine the current missing sample count 
   *  
   * @return the current missing sample count 
   */ 
  public final long getMissingSampleCount() 
  { 
    return Math.max( 0L, minSampleCount.get() - availableSampleCount.get() );
  } 
   
  /** 
   * Method to calculate the available sample count for collecting task 
   *  
   * @param currentRecordCount 
   *          the current record count in database 
   * @param timeStamp 
   *          the current time stamp 
   */ 
  private final void updateFields( long currentRecordCount, long timeStamp )
  { 
    // update available sample count 
    long cnt =
        Math.max( 0L, Math.min( maxSampleCount.get(), currentRecordCount ) );
    availableSampleCount.set( cnt );
     
    // store the record count reduced by the available samples, which 
    // can be consumed by the controlled transfer thread 
    lastRecordCount = currentRecordCount - availableSampleCount.get();
     
    // update time stamp 
    lastTimeStamp.set( timeStamp );
  }