But, instead of using nano-time, a counter (or discriminator as I've seen it called) is used when collisions occur in the same millisecond: private static long previousTimeMillis = System. This code is inspired by Nuriyev's answer. You can do something like: Uuid::uuid3 (Uuid::NAMESPACEDNS, 'TEST STRING')->toString () It uses md5 hashing in this example. ![]() You can generate the same UUID for the given namespace + string. It is usually good to take into account the current time and something else such as the current thread id, process id, ip. Java Uuid Generator (JUG) JUG is a set of Java classes for working with UUIDs: generating UUIDs using any of standard methods, outputting efficiently, sorting and so on. mika This PHP UUID library is somewhat equivalent. Side note: nanoseconds should not be used to represent the current time because their starting point is not fixed in time and because they are recycled when they reach the maximum. Efficient method to generate UUID String in Java (UUID.randomUUID().toString() without the dashes) 118. For some applications we need only numbers. But this consists of letters and numbers. The nano & ~9223372036854251520L part takes the current nanoseconds and sets the first 44 bytes to 0, leaving only the right 20 bits which represent nanoseconds up to one millisecond (999999 nanos) JUG generates UUIDs according to the IETF UUID draft specification. To get it, shift the current milliseconds 20 bits left to allocate space for nanoseconds and then overlay it with the nanoseconds: (System.currentTimeMillis() << 20) | (System.nanoTime() & ~9223372036854251520L) Java UUID Generator It can be used either as a component in a bigger application, or as a standalone command line tool. But in many cases a number may be unique enough for a specific use.įor example, a timestamp with the nanosecond precision is often good enough. ![]() As the others have written, long does not have enough space for a unique number.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |