Java AtomicLong getNowMicrosUtc()

Here you can find the source of getNowMicrosUtc()

Description

Return wall clock time, in microseconds since Unix Epoch (1/1/1970 UTC midnight).

License

Open Source License

Declaration

public static long getNowMicrosUtc() 

Method Source Code

//package com.java2s;
/*//from ww  w .j a v a 2s.co  m
 * Copyright (c) 2014-2015 VMware, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.  You may obtain a copy of
 * the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, without warranties or
 * conditions of any kind, EITHER EXPRESS OR IMPLIED.  See the License for the
 * specific language governing permissions and limitations under the License.
 */

import java.util.concurrent.atomic.AtomicLong;

public class Main {
    private static AtomicLong PREVIOUS_TIME_VALUE = new AtomicLong();

    /**
     * Return wall clock time, in microseconds since Unix Epoch (1/1/1970 UTC midnight). This
     * functions guarantees time always moves forward, but it does not guarantee it does so in fixed
     * intervals.
     *
     * @return
     */
    public static long getNowMicrosUtc() {
        long now = System.currentTimeMillis() * 1000;
        long time = PREVIOUS_TIME_VALUE.getAndIncrement();

        // Only set time if current time is greater than our stored time.
        if (now > time) {
            // This CAS can fail; getAndIncrement() ensures no value is returned twice.
            PREVIOUS_TIME_VALUE.compareAndSet(time + 1, now);
            return PREVIOUS_TIME_VALUE.getAndIncrement();
        }

        return time;
    }
}

Related

  1. getMethodExecuteCount()
  2. getMeUniqueString(Calendar cal)
  3. getMmapBufferUsage()
  4. getNativeSeed(Random rand)
  5. getNextTestIndexName()
  6. getRAMUniqueID()
  7. getRandomTopicName()
  8. getSequenceNumber()
  9. getTempDirName()