I'm developing an application (Java & JavaFX) that writes/reads data (a file). The problem is I don't want to restrict user to run only one instance (of my app) at a time, as I really can't think of reliable way of doing that so it works on both Windows and Linuxes (e.g. server), heard of sockets and files - both are defective IMO. As user is able to run multiple instances, writing/reading data (from a file) seems really messy, because there's no guarantee that file locking will work reliably on Windows and Linuxes (FileLock documentation - click here).

To sum up: I can't restrict multiple instances of my app, but that leads to problem with writing/reading data (from a file).

Is there anything I missed? Maybe there's some other way to solve my problem I can't think of? How the "big" popular programs handle that?

  • What data are you reading/writing to the file? Can different instances of the app use different files?
    – mmathis
    Mar 10 at 17:00
  • @mmathis progress, objectives of task, results of request, etc. A lot of data. About different files - I'm not sure how to handle it, all of the instances will be the same, so theoretically, if two instance will try to create the same file (as both instances will see no files when they start), that will lead to a data integrity problems
    – Wiktor
    Mar 10 at 17:25
  • If you can't trust OS to do file locking you really out of luck (short of writing your own OS "with blackjack and ..." :) ). You may want to reconsider your stance on it - chance that thousands of people worked on Linux/Unix/Windows/Mac OSes got file access/sharing wrong and you will be able singlehandedly solve the problem is somewhat low. Mar 10 at 19:15
  • You can use a mutex to make sure only one instance will run (look it up) and you can open files for writing, thus inhibiting other processes to change them. Mar 10 at 20:38
  • Is is expected behavior that multiple instances will be running and accessing the same file? Or would normal usage of the app imply that each user runs a single instance and each instance accesses a different file? Mar 23 at 14:29

1 Answer 1


When the application starts, generate a new filename based on a GUID or some other guaranteed-unique identifier. That particular instance of the application will then always read and write to a unique file, without worrying about file locks or conflicts. When the application is closed, it can delete the file.

If the application needs to resume reading/writing that file the next time it opens, however, you'll need to handle the file conflicts. Depending on how often the file is used and how many instances of the application you realistically expect to be open simultaneously, you can just add some re-try logic to your file IO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.