System.nanoTime()保证返回唯一值吗? [英] Is System.nanoTime() guaranteed to return unique values?

查看:182
本文介绍了System.nanoTime()保证返回唯一值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多线程的Java程序,可以在几秒钟内创建数百个临时文件。这些文件放在 / tmp 中,并使用 System.nanoTime()来命名。 b

是否保证文件名是唯一的?

解决方案

不,不能保证每个调用 System.nanoTime()将会返回一个唯一的值。
$ b 使用 File.createTempFile() Files.createTempFile() 来代替。它们只是为了这个目的而设计的,并且会为您生成唯一的文件名。


I have a multi-threaded Java program that creates hundreds of temporary files in seconds. The files are placed in /tmp and are named using System.nanoTime().

Are the file names guaranteed to be unique?

解决方案

No, there is no guarantee that every call to System.nanoTime() will return a unique value.

Use File.createTempFile() or Files.createTempFile() instead. They are designed for just this purpose, and will generate unique file names for you.

这篇关于System.nanoTime()保证返回唯一值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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