How to create a 64 bit Unique Integer in Java

john

I need to create a 64 bit unique integer in Java so that collision chances are low. The system is not distributed, so collisions between different computers are not a problem.

Is there any way, we can create a 64 bit integer in Java which is always Unique?

As of now I am using -

long number = System.nanoTime();

Is this the right way to generate 64 bit Unique Integer in Java or is there anything else I can try?

UPDATE:-

How about doing this way? Will this be unique?

UUID number = UUID.randomUUID();
long uniqueNumber = number.timestamp();
Peter Lawrey

If you need the numbers to be unique in one process, robust between restarts, you can use a simple AtomicLong and a timer.

private static final AtomicLong TS = new AtomicLong();
public static long getUniqueTimestamp() {
    long micros = System.currentTimeMillis() * 1000;
    for ( ; ; ) {
        long value = TS.get();
        if (micros <= value)
            micros = value + 1;
        if (TS.compareAndSet(value, micros))
            return micros;
    }
}

This will give you a unique "timestamp" with a millisecond accuracy but can only handle 1000 ids per millisecond without getting ahead of the actual time. This works fine on restart as the time will jump past previous values (again assuming you have less than one million per second on average)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to create unique random integer?

How to printf a 64-bit integer as hex?

How big can a 64 bit unsigned integer be?

How to convert integer to unsigned 64-bit integer

Create Minimum perfect hash for sparse 64-bit unsigned integer

How to create a 128-bit integer literal

How to create 24 bit unsigned integer in C

overflow warnings for long to integer conversions after updating to 64 bit java

php 64bit integer overflow differs from Java implmention

How to create 32 bit version of Python code from 64 bit

How to put 32-bit signed integer into higher 32 bits of 64-bit unsigned integer?

How to turn a double to a 64-bit integer bit by bit using unions

How to map all 64-bit integers to a different 64-bit integer in a 1-to-1 fashion

How to get a 64-bit integer back from Presto in R?

How to generate random 64-bit unsigned integer in C

How to represent 64-bit integer in VB6?

How to check availability of 64-bit integers (for `use integer`)?

How should a 64-bit integer literal be represented in C++?

How to convert and Integer to an 64-bit byte representation in python

How can one read an integer bit by bit in Java?

Format comma for a bit 64 integer

Output 64 bit integer with hexdump

How to generate 64 Bit entropy token in Java

How to Generate Unique ID in Java (Integer)?

Java - How to check if entered Integer is unique

How to add 2^63 to a signed 64-bit integer and cast it to a unsigned 64-bit integer without using 128-bit integer in the middle

How can I extend my code do divide by a 64bit integer? (128bit / 64bit)

How does Rust's 128-bit integer `i128` work on a 64-bit system?

64 bit Centos Java JVM unable to create a native thread

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive