如何检测打开文件句柄的文件是否已在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中做到这一点?
谢谢,
-PeterI 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
最满意答案
这是因为共享
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 aSimpleDateFormat
: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 usepayDayFormat
:Date billDate; synchronized (payDayFormat) { billDate = payDayFormat.parse(repayDay); }
Create a new instance of
SimpleDateFormat
inside the method;- Make
payDayFormat
aThreadLocal<SimpleDateFormat>
, so that each thread has its own copy.
相关问答
更多-
电脑二级考试 是考access 还是VPF[2022-06-08]
access -
有效地计算闰日(Efficiently calculate leap days)[2023-02-21]
如果您对分析谨慎,可以避免所有循环。 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 ...
-
在java中并发访问静态方法,是否需要同步?(concurrent access to static methods in java, is synchronization needed?)[2022-02-06]
您不需要同步该方法。 局部变量是在当前线程的“内存空间”中创建的,并且没有任何其他线程可以访问它(来自上面显示的内容)。 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' ... -
并发访问用于计算逾期天数的静态方法(Concurrent access to static methods which is used to calculate overdue days)[2023-12-09]
这是因为共享payDayFormat ,它是一个SimpleDateFormat : 日期格式未同步。 建议为每个线程创建单独的格式实例。 如果多个线程同时访问格式,则必须在外部进行同步。 它与Jodatime一起使用的原因是它的日期格式化程序没有共享的可变状态,因为它是明智的。 所以,您可以: 添加synchronized到方法签名; 在您使用payDayFormat地方添加一个synchronized块: Date billDate; synchronized (payDayFormat) { bi ... -
并发执行静态和非静态同步方法的问题?(Problem with concurrent execution of static and non-static synchronized methods?)[2022-08-25]
多个线程访问的静态和非静态数据由不同级别的锁处理。 访问非静态方法的线程共享对象级锁(每个对象),在这里访问静态方法需要类级锁(每个类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! ...