Java AtomicLong getForeverUniqueID()

Here you can find the source of getForeverUniqueID()

Description

PLEASE use only if REALLY needed.
Best effort is taken to make it "globally unique" without any persisting media by instantiating it by System.currentTimeMilis * 64, so that after a restart of the vm, the counter advances 64000 units per second which should be enough that that value is never exceeded by the usage of that ID (100 concurrent users which can consume 640 unique id per each second, and: even if exceeded, after a restart of a vm (assumed time at least 10secs), a loss of 10*64000 = 640000 can be caught up

if you just need a counter which is unique within the virtual machine, but does not need to be unique if the sessions are persisted and the vm is restarted, then use @see getRAMUniqueID() returns a unique id; even if the system is restarted.

License

Apache License

Return

long

Declaration

public static long getForeverUniqueID() 

Method Source Code

//package com.java2s;
/**// w ww  .  j  a  v  a  2 s  . c o m
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <hr>
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* This file has been modified by the OpenOLAT community. Changes are licensed
* under the Apache 2.0 license as the original file.  
* <p>
*/

import java.util.concurrent.atomic.AtomicLong;

public class Main {
    private static final AtomicLong timeuniqueId = new AtomicLong(System.currentTimeMillis() * 64);

    /**
     * PLEASE use only if REALLY needed.<br>
     * 
     * Best effort is taken to make it "globally unique" without any persisting media by instantiating it by System.currentTimeMilis * 64, so that
     * after a restart of the vm, the counter advances 64000 units per second which should be enough that that value is
     * never exceeded by the usage of that ID (100 concurrent users which can consume 640 unique id per each second, and: even if exceeded, after a restart of a vm
     * (assumed time at least 10secs), a loss of 10*64000 = 640000 can be caught up
     * <br>
     * <br>
     * if you just need a counter which is unique within the virtual machine, 
     * but does not need to be unique if 
     * the sessions are persisted and the vm is restarted, then use @see getRAMUniqueID()
     * 
     * returns a unique id; even if the system is restarted.
     * 
     * @return long
     */
    public static long getForeverUniqueID() {
        return timeuniqueId.incrementAndGet(); //o_clusterNOK synchronized check what of data generated with that long is persisted
    }
}

Related

  1. getAndAddRequest(AtomicLong requested, long n)
  2. getAndAddRequest(AtomicLongFieldUpdater requested, T object, long n)
  3. getAtomicLongArraySameLengthAsList(List list)
  4. getBench(Map times, int amount)
  5. getDateSafe()
  6. getGlobalTimeStamp()
  7. getList(AtomicLongArray atomicLongArray)
  8. getMessageId()
  9. getMethodExecuteCount()