用Java线程安全地创建和删除文件 [英] Thread safe creating and deleting file in Java

查看:724
本文介绍了用Java线程安全地创建和删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方法Files.createFile()和Files.delete()线程安全吗?我已经阅读了文档,其中createFile()始终是原子操作,而delete()不是.我应该以某种方式在Java应用程序中同步这些块吗?原子操作对多任务任务意味着什么?

解决方案

a.原子操作对多任务任务意味着什么?

在多线程原子性的上下文中,线程以某种方式执行任务的能力,以使其他线程在由该线程执行该任务时,对该任务的状态变量显然没有副作用.

File.createNewFile():-对于此方法,当线程将要执行此方法时,状态为文件的存在或不存在.可以说,当线程执行此方法时,该文件不存在.现在,让我们说这种方法需要5毫秒的时间来执行和创建文件.因此,根据原子性的概念,在这5毫秒内没有其他线程应该能够创建相同的文件(之前不存在),否则此线程关于文件状态的第一个假设将改变,因此输出也将改变. /p>

因此,在这种情况下,执行线程通过在要创建文件的目录上获得写锁定来做到这一点.

Files.delete():-此方法的Java文档说

相对于此方法,该方法可能不是原子的 其他文件系统操作.如果文件是符号链接,则 符号链接本身而不是链接的最终目标将被删除.

上面的语句表示此操作也是原子操作,但是如果在符号链接上调用此方法,则会删除该链接而不是文件.这意味着原始文件存在,并且其他线程可以对该文件进行文件系统操作.

要确定文件是否为符号链接,请参见参考: 确定符号链接

b.我应该以某种方式在我的Java应用程序中同步这些块吗?

在两种情况下都不需要处理任何多线程方案. 但是,您可以使用上面链接中提到的方法来确定符号链接并根据需要单独处理. 但是肯定不需要从头开始同步.

Are methods Files.createFile() and Files.delete() thread safe? I have read in documentation that createFile() always an atomic operation but delete() is not. Should I somehow synchronize these blocks in my Java application and how? What atomic operation means for multihreading task?

解决方案

a. What atomic operation means for multihreading task?

In context of multi-threading atomicity is the ability of a thread to execute a task in such a manner so that other threads have apparently no side-effect over the state varibles of that task when it was being executed by this thread.

File.createNewFile() :- For this method the state is existence or non existence of the file, when the thread was about to execute this method. Lets say that when this method was being executed by the thread the file did not exist. Now lets say that this method takes 5 ms of time to execute and create the file. So according to the concept of Atomicity no other thread should be able to create the same file(which was not existing before) during these 5ms otherwise the very first assumption of this thread about the state of the file will change and hence the output.

So in this case the executing thread does-this by obtaining a write lock over the directory where file is to be created.

Files.delete():- The Java doc for this method says

this method may not be atomic with respect to other file system operations. If the file is a symbolic link, then the symbolic link itself, not the final target of the link, is deleted.

the above statement says that this operation is also atomic but in case if this method is invoked on a symbolic link, the link is deleted and not the file. Which implies that the original file exists and file system operations on that file are feasible by other threads.

to determine if a file is a symlink see the reference:- determine symlink

b. Should I somehow synchronize these blocks in my Java application and how?

You need not handle any multi-threading scenarios in both the cases. However you can use the method mentioned in the link above to determine symlinks and handle that separately as you would wish. But no synchronization is required from your end for sure.

这篇关于用Java线程安全地创建和删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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