public class ReconnectInterval
{
  private AtomicInteger m_AtomicCount = new AtomicInteger(0);
  private int m_Max = 12;
  private double A = Math.sqrt(5) / 5;
  private double B = (1 + Math.sqrt(5)) / 2;
  private double C = (1 - Math.sqrt(5)) / 2;

  long getInterval()
  {
    int nValue = m_AtomicCount.incrementAndGet();

    if (nValue / m_Max > 0)
    {
        nValue = m_Max;
    }
    long lFibonacci = fibonacci(nValue);
    return lFibonacci * 1000;
  }

  void reset()
  {
    m_AtomicCount.set(0);
  }

  long getRetryCount()
  { 
    return m_AtomicCount.get();
  }

  /**
   * @param n
   * @return 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,
   *         610,987,1597,2584,4181,6765,10946,17711,28657,46368……
   */
  private long fibonacci(int n)
  {
    return (long) (A * (Math.pow(B, n) - Math.pow(C, n)));
  }
}

arrow
arrow
    文章標籤
    Android
    全站熱搜
    創作者介紹
    創作者 你是誰 的頭像
    你是誰

    你是誰的部落格

    你是誰 發表在 痞客邦 留言(0) 人氣()