Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is java.util.UUID thread safe?

I am asking this question because of following observations

  1. getting this stack trace in thread dump in highly multi threaded environment

    "http-80-200" daemon prio=10 tid=0x00002aaab4981000 nid=0x7520 waiting \  for monitor entry [0x000000004fec7000]    java.lang.Thread.State: BLOCKED (on object monitor)     at java.security.SecureRandom.nextBytes(SecureRandom.java:433)     - waiting to lock <0x00000000c00da220> (a java.security.SecureRandom)     at java.util.UUID.randomUUID(UUID.java:162) 
  2. found this link

    http://bugs.sun.com/view_bug.do?bug_id=6611830

if UUID is not thread safe, please suggest any other library if it exist.

like image 411
Abhishek Gayakwad Avatar asked Aug 27 '11 04:08

Abhishek Gayakwad


People also ask

Is Java Util thread-safe?

In fact, all collection classes (except Vector and Hashtable) in the java. util package are not thread-safe. The only two legacy collections are thread-safe: Vector and Hashtable.

Is Java UUID cryptographically secure?

Its slower than Random, but much more secure. Take a look at the source code for UUID a few posts up, its already using a secureRandom. It is using SecureRandom, but it is removing some of the randomness.

Is UUID random secure?

Well, the source code shows UUID. randomUUID uses SecureRandom . As you can see, you can use either, but in a secure UUID you have 6 non-random bits, which can be considered a disadvantage if you are picky.

Is Java UUID a string?

The third static method returns a UUID object given the string representation of a given UUID: UUID uuid = UUID. fromString(String uuidHexDigitString); Let's now look at how a UUID is structured.


1 Answers

UUID is immutable so it's potentially thread safe, but apparently there was some evil caching going on in some accessors that made it unsafe (that bug is fixed now).

But your thread dump just says that a thread is waiting for a lock at SecureRandom.nextBytes, which is used by the UUID.randomUUID factory, which definitely is thread-safe. It's what's supposed to happen when several threads call it simultaneously, as far as I can tell.

like image 196
Joonas Pulakka Avatar answered Sep 17 '22 08:09

Joonas Pulakka