java - Is File instance thread safe? -


I've read that new file ("path") does not physically make a file on disk is . Although it is said in the API:

Examples of this class may or may not be an actual file-system object such as a file or directory. If such an object specifies, that object remains in a partition. A partition is a operating system-specific part of storage for a file system, a single storage device (eg a physical disk-drive, flash memory, CD-ROM) can have multiple partitions < P> So I'm curious if such a code is safe in a multithrred environment:

  file file = new file ("myfile.zip"); // Take some action with file (fill it with content) file.save (anywhere); // Simply show that I save it after a number of tasks   

For example, Thread 1 comes here, creates an example and is starting to work Meanwhile, In thread 2, it is interrupted, the example makes by the same name (myfile.zip) and performs some other operations. After that he saved the file.

I need to make sure that they do not work with the same file and the previous thread overwrites the previous one to save the file.

No, the file does not keep locked and is not safe for the pattern you describe. File should be locked or placed in the wrapper class.

If you provide a bit more code, then one can definitely help you find a suitable pattern.

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -