是createTempFile线程安全的? [英] Is createTempFile thread-safe?

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

问题描述



调用createTempFile(类java.io.File)的两个线程是否有可能获得相同的临时文件? / p>

解决方案

获得答案的最佳方法是查看源代码。首先在createTempFile中没有任何同步,但为了生成临时文件名,它使用的是ThreadSafe的SecureRandom。
然后,除非你真的不幸,你的文件将总是得到一个不同的名字。

最重要的是,createTempFile实现循环,产生新的文件名,直到文件被创建。当然文件创建委托给本地文件系统操作,我们可以假设是线程安全的。


I'm using Java 6.

Is it possible that two threads calling createTempFile (of the class java.io.File) get the same temp file?

解决方案

Best way to get your answer is to look at the source code. At first there isn't any synchronization in createTempFile, but to generate the temp file name, it is using SecureRandom which is ThreadSafe. Then unless you are really unlucky, your file will always get a different name.

On top of that, createTempFile implementation is looping, generating new file name, until the file has been created. The file creation of course is delegated to the native file system operation which we may assume is threadsafe..

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

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