Does the new file ("...") block the file?

I read that new File("path")physically does not create a file on disk. Although the API says:

Instances of this class may or may not indicate the actual file system object, such as a file or directory. If it denotes such an object, then this object is in the section. A partition is part of the operating system storage for the file system. A single storage device (e.g., a physical drive, flash memory, CD-ROM) may contain several partitions.

So I'm curious if it is safe to have such code in a multi-threaded environment:

File file = new File( "myfile.zip" );
// do some operations with file (fill it with content)
file.saveSomewhere(); // just to denote that I save it after several operations

For example, thread1 comes here, instantiates, and starts performing operations. Meanwhile, thread2 interrupts it, creates its instance with the same name (myfile.zip), and performs some other operations. After that they save the file.

I need to be sure that they do not work with the same file, and the last thread saving the file overwrites the previous one.

+3
source share
2 answers

No, the file does not save the lock and is not safe for the template you describe. You must either lock or save the file in some wrapper class.

If you provide a little more of your code, someone can certainly help you find a suitable template.

+2
source

, , , , . : , - , , ( ).

, , , , , , - .

0

All Articles