*
The moose likes I/O and Streams and the fly likes Can File.exists and File.delete have a race condition? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Can File.exists and File.delete have a race condition?" Watch "Can File.exists and File.delete have a race condition?" New topic
Author

Can File.exists and File.delete have a race condition?

Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Hi

Can File.exists and File.delete have a race condition?

Let's say thread-1 deletes a file and thread-2 checks for the existence of the same file. Now do I need to ensure that this does not happen simultaneously or does Java/OS take care of this by itself.

Regards


Thanks and Regards
meghala devi
Greenhorn

Joined: Oct 07, 2010
Posts: 3
Hi Sathya

Can synchronizing the block of code help?

This ensures that only one thread accesses the file at a time
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

It depends on the file system, but assume there is no synchronization. If one thread or process* deleted the file mere nanoseconds after File.exists() has returned true the file no longer exists even though your program thinks it does. This isn't something you can do much about I fear.


* After all, the file can also be deleted from Explorer, or another program.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
GeeCON Prague 2014
 
subject: Can File.exists and File.delete have a race condition?