问题描述
调用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线程安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!