java-uuid-generator icon indicating copy to clipboard operation
java-uuid-generator copied to clipboard

Suggestion to add a static method to generate a random based UUID using ThreadLocalRandom (requires JDK 7)

Open tehnic-take3 opened this issue 3 years ago • 6 comments

I suggest to add a static method like the following to UUIDUtil (or any other appropriate class):

public static UUID generateThreadLocalRandomUUID() { ThreadLocalRandom rnd = ThreadLocalRandom.current(); return UUIDUtil.constructUUID(UUIDType.RANDOM_BASED, rnd.nextLong(), rnd.nextLong()); }

Using a generator created by Generators.randomBasedGenerator(ThreadLocalRandom.current()) is probably not a good idea when working in an environment with thread pools (like Java/Jakarta EE), so i came to this solution.

Best regards, Robert

tehnic-take3 avatar Apr 06 '21 12:04 tehnic-take3

Hi there! Thank you for the suggestion!

I'll have to think about this a bit wrt API; and also see what is the minimum JDK expectation (ThreadLocalRandom seems to have been added in Java 1.7/7).

cowtowncoder avatar Apr 07 '21 03:04 cowtowncoder

Sort of orthogonal but If the required JVM version is bumped to 9 or later I recommend you use System.Logger instead of SLF4J.

To be honest I'm surprised this library uses logging.

agentgt avatar Mar 02 '22 17:03 agentgt

I doubt there's much point in bumping minimum JDK past 8 in near future.

As to logging, yes it is not used for regular operation (definitely not on per-uuid-generated basis) but for some specific failure condition... I'd have to check it but I think it was for file-backed locking, if used to store state across invocations (last "reserved" timestamp from time/location based variant).

You can also follow discussions wrt evolution of logging dependency, f.ex #32.

cowtowncoder avatar Mar 03 '22 21:03 cowtowncoder

Ok, couple of notes:

  • UUIDUtil is probably not an optimal place (although I guess I could). But maybe rather add straight into Generators
  • Or, could perhaps figure out a way to encapsulate things to allow proper generator instance that hides access?
  • ... alas, Jug 4.x is still Java 6 and ThreadLocalRandom seems to JDK 7 addition.

The last point would keep this out of 4.0.x. But if we were to increase JDK baseline to 8 that wouldn't be a problem. Besides although java source/target is defined as 1.6, I am not 100% sure there might not already be accidental JDK 7 dependencies.

Still, I need to think about this a bit now.

cowtowncoder avatar Jun 18 '22 04:06 cowtowncoder