is java.util.UUID thread safe?

2020-05-24 20:58发布

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.

2条回答
够拽才男人
2楼-- · 2020-05-24 21:14

Uuid is thread safe however there is another library called JUG which is more efficient in performance.

Source :http://www.dcalabresi.com/blog/java/generate-java-uuid-performance/

查看更多
对你真心纯属浪费
3楼-- · 2020-05-24 21:26

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.

查看更多
登录 后发表回答