Atomic motion and many processes performing it simultaneously

In my application, I have a scheduled task that keeps track of the directory for files with a specific extension. These files are processed further. There may be several processes running this application, and so that multiple instances process the same file, I change its extension, so it did not list another time using Files.movewith the option ATOMIC_MOVE. There are chances that several instances of the application will try to execute this method at the same time - my question is: what happens next? Should I expect any exceptions?

+4
source share
1 answer

: "", java.nio.file.NoSuchFileException, .

try {
    Files.move(previousPath, renamedPath, StandardCopyOption.ATOMIC_MOVE);
}
catch (NoSuchFileException e) {
    /* Another process has already processed that file. Ignore and move on. */
}
catch (IOException e) {
    /* Must handle that one: something else has failed */
}

Linux macOS , , UUID . NoSuchFileException, .

. , (NFS...), .

+1

Source: https://habr.com/ru/post/1680076/


All Articles