Async file saving

Discussion in 'Plugin Development' started by rsod, Jul 7, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    rsod

    Currently I have following code responsible for one of YamlConfiguration files:
    Code:java
    1. private FileConfiguration RegisteredNames = null;
    2. private File RegisteredNamesFile = null;
    3. private boolean needSave = false;
    4. public void reloadRegisteredNames() {
    5. if (RegisteredNamesFile == null) {
    6. RegisteredNamesFile = new File(getDataFolder(), "RegisteredNamesData.yml");
    7. }
    8. RegisteredNames = YamlConfiguration.loadConfiguration(RegisteredNamesFile);
    9. }
    10.  
    11. public FileConfiguration getRegisteredNames() {
    12. if (RegisteredNames == null) {
    13. this.reloadRegisteredNames();
    14. }
    15. return RegisteredNames;
    16. }
    17.  
    18. public void saveRegisteredNames() {
    19. if (RegisteredNames == null || RegisteredNamesFile == null) {
    20. return;
    21. }
    22. try {
    23. getRegisteredNames().save(RegisteredNamesFile);
    24. this.getLogger().info("Database saved");
    25. } catch (IOException ex) {
    26. this.getLogger().log(Level.SEVERE, "Could not save config to " + RegisteredNamesFile, ex);
    27. }
    28. }
    29.  
    30. @Override
    31. public void onEnable(){
    32. getServer().getPluginManager().registerEvents(this, this);
    33. reloadRegisteredNames();
    34. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    35. @Override
    36. public void run() {
    37. if(!needSave) return;
    38. saveRegisteredNames();
    39. needSave = false;
    40. }}, 6000, 6000);
    41. }

    My target is moving file saving to separate thread.
    Will it be safe and enough to simply replace SyncRepeatingTask to AsyncRepeatingTask? Let's also take in mind that file contains extremely important information, and losing/corrupting this file can cause very bad things.
     
  2. Offline

    Jogy34

    Considering the AsyncRepeatingTask is deprecated, because when working with any gameplay stuff with it there is a possibility of seriously screwing things up if it's not in sync with the main thread. And it was depriated a few versions ago I don't really know if it will work. You can easily make your own thread that does this though but make sure that anywhere that you are accessing the file in the thread that you don't attempt to access it anywhere else in your code at the same time as that can screw it up. You can just make a flag that says whether or not you are using it in the thread. Here's some simple code that will do what you are looking for in a thread:
    Code:java
    1.  
    2. new Thread(new Runnable()
    3. {
    4. @Override
    5. public void run()
    6. {
    7. while(true)
    8. {
    9. accessingFileFlag = true;
    10. //do whatever with file here
    11. accessingFileFlag = false;
    12. try
    13. {
    14. this.sleep(60000); //will cause this thread to sleep for 1 minute as the number is in milliseconds
    15. }
    16. catch(Exception e) { e.printStackTrace()} //This means that the thread was interrupted while it was 'sleeping'
    17. }
    18. }
    19. }).start();
     
  3. Offline

    slayr288

    rsod
    Yes, it's safe to save data to disk/a database off the main thread. Make sure the data is in memory and you're not getting it from the server though.
     
Thread Status:
Not open for further replies.

Share This Page