是java.util.UUID线程安全吗? [英] is java.util.UUID thread safe?

查看:1247
本文介绍了是java.util.UUID线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下观察,我问这个问题

I am asking this question because of following observations


  1. 在高度多线程环境中的线程转储中获取此堆栈跟踪

  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)


  • 找到此链接

  • found this link

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

    if UUID不是线程安全的,请建议任何其他库(如果存在)。

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

    推荐答案

    UUID是不可变的,因此它可能是线程安全的,但是显然在某些访问者中有一些邪恶缓存正在进行,这使得它不安全(错误现在修复了。)

    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).

    但你的线程转储只是说一个线程正在等待锁定 SecureRandom.nextBytes ,由 UUID.randomUUID 工厂使用,肯定 是线程安全的。据我所知,当多个线程同时调用它时,应该会发生这种情况。

    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.

    这篇关于是java.util.UUID线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆