首页 \ 问答 \ 如何检测打开文件句柄的文件是否已在Windows上使用Perl删除?(How can I detect if the file for an open filehandle has been deleted on Windows using Perl?)

如何检测打开文件句柄的文件是否已在Windows上使用Perl删除?(How can I detect if the file for an open filehandle has been deleted on Windows using Perl?)

我有一个打开文件句柄的过程。 我需要检测这个文件是否已被另一个进程删除(可能有一个文件名称与它相同)。 在UNIX上,我通过stat比较文件句柄的inode和文件路径,但这在Win32上不起作用。 我如何在Perl中做到这一点?

谢谢,
-Peter


I have a process with an open filehandle to a file. I need to detect if this file has been deleted by another process (there may be a file with the same name in its place). On UNIX I was comparing the inodes of my filehandle and the file-path via stat, but this doesn't work on Win32. How can I do this in Perl?

Thanks,
-Peter


原文:https://stackoverflow.com/questions/682404
更新时间:2024-03-15 10:03

最满意答案

这是因为共享payDayFormat ,它是一个SimpleDateFormat

日期格式未同步。 建议为每个线程创建单独的格式实例。 如果多个线程同时访问格式,则必须在外部进行同步。

它与Jodatime一起使用的原因是它的日期格式化程序没有共享的可变状态,因为它是明智的。

所以,您可以:

  • 添加synchronized到方法签名;
  • 在您使用payDayFormat地方添加一个synchronized块:

    Date billDate;
    synchronized (payDayFormat) {
      billDate = payDayFormat.parse(repayDay);
    }
    
  • 在方法内创建一个SimpleDateFormat的新实例;

  • 使payDayFormat成为ThreadLocal<SimpleDateFormat> ,以便每个线程都有自己的副本。

It's because of sharing payDayFormat, which is a SimpleDateFormat:

Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.

The reason it works with Jodatime is that its date formatters don't have shared mutable state, because it's sensible.

So, you can either:

  • Add synchronized to the method signature;
  • Add a synchronized block just around where you use payDayFormat:

    Date billDate;
    synchronized (payDayFormat) {
      billDate = payDayFormat.parse(repayDay);
    }
    
  • Create a new instance of SimpleDateFormat inside the method;

  • Make payDayFormat a ThreadLocal<SimpleDateFormat>, so that each thread has its own copy.

相关问答

更多
  • 如果您对分析谨慎,可以避免所有循环。 CountLeapDays 算法评论 /* ** Count the number of 4-year leap years. ** Count the number of 100-year non-leap years. ** Count the number of 400-year leap years. ** ** Switchover between 20th and 21st centuries: 2000 was a leap year. ...
  • 不,只是静态不会使功能变得纯粹。 在纯粹的函数式编程中,函数的结果应该仅取决于它们的参数,而不考虑全局状态。 静态功能可以轻松访问和修改全局状态。 任何有用的纯函数都必须返回一个值。 静态函数可以并经常被声明为无效的,这对于纯函数是没有意义的。 纯函数应该每次都为相同的输入产生相同的结果。 任何使用静态计数器的静态函数都可能违反该规则。 例如,在Java中,流对象本质上是功能性的。 它们的函数(如filter())不是静态的,但通过生成新流而不是修改原始流对象来维护流数据的不变性。 这就是说,静态函数更容易 ...
  • 这是一种适用于月份和年份的方法: let calendar = NSCalendar.currentCalendar() let date = NSDate() // Calculate start and end of the current year (or month with `.Month`): var startOfInterval : NSDate? var lengthOfInterval = NSTimeInterval() calendar.rangeOfUnit(.Year, sta ...
  • 谢谢PriceCheaperton! 这里回答了一个稍微宽泛的问题,也可以给出对给定总和的部分尺寸的限制。 在上面链接顶部的Python解决方案中,您将修改对此函数的调用以解决您的特定示例(我不知道15..30是否有效,否则您需要以相同的方式完成列表已经开始了): subset_sum([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15..30],30) 最诚挚的问候,Mats Thank you PriceCheaperton! A somewhat wider question ...
  • 您不需要同步该方法。 局部变量是在当前线程的“内存空间”中创建的,并且没有任何其他线程可以访问它(来自上面显示的内容)。 You don't need to synchronize that method. The local variable gets created in the current thread's "memory space" and there is no way that it will get accessed by any other thread (from what you' ...
  • 这是因为共享payDayFormat ,它是一个SimpleDateFormat : 日期格式未同步。 建议为每个线程创建单独的格式实例。 如果多个线程同时访问格式,则必须在外部进行同步。 它与Jodatime一起使用的原因是它的日期格式化程序没有共享的可变状态,因为它是明智的。 所以,您可以: 添加synchronized到方法签名; 在您使用payDayFormat地方添加一个synchronized块: Date billDate; synchronized (payDayFormat) { bi ...
  • 多个线程访问的静态和非静态数据由不同级别的锁处理。 访问非静态方法的线程共享对象级锁(每个对象),在这里访问静态方法需要类级锁(每个类1个) do-while循环没有问题,因为 BadDesign.changeDataViaStaticMethod(5); //needs BadDesign Class lock. BadDesign.showSensitiveDataStatic(); //does not need any lock 和 bd.changeDat ...
  • 您可能希望将函数mysub定义为staticmethod ,因此您可以将其用作“独立”函数: class Myclass1(object): def __init__(self,d):#, dict_value): self.d=d def myadd(self): b=2 return b @staticmethod def mysub(u): #Note that self is not an argument! ...

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)