title: Java Multi-threading prevent two functions running at same time
body: <p>I am creating a multi-threaded server and I created a class to manage users, but I noticed that my UserManager class could probably cause errors when one thread is reading the file and other is writing to the file, how to prevent it?</p>
  
  <pre><code>package server.questiongiver;
  
  import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileReader;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.io.UnsupportedEncodingException;
  import java.net.Socket;
  import java.util.ArrayList;
  
  import server.engine.CustomLog;
  
  public class UserManager {
  
      public static User loadUser(String id, Socket s) {
          User user = null;
          if (id == null) {
              return user;
          }
          File f = new File("users.dat");
          if (f.isFile() &amp;&amp; f.canRead()) {
              try (BufferedReader br = new BufferedReader(new FileReader(f))) {
                  String line;
                  while ((line = br.readLine()) != null) {
                      if (line.equals("[" + id + "]")) {
                          user = new User(s);
                          user.id = id;
                          user.password = br.readLine().split("-separator-")[1];
                          user.username = br.readLine().split("-separator-")[1];
                          break;
                      }
                  }
              }
              catch (IOException ex) {
                  CustomLog.error(ex.getMessage());
              }
          }
          return user;
      }
  
      public static ArrayList&lt;User&gt; loadAllUsers() {
          File f = new File("users.dat");
          ArrayList&lt;User&gt; users = new ArrayList();
          if (f.isFile() &amp;&amp; f.canRead()) {
              try (BufferedReader br = new BufferedReader(new FileReader(f))) {
                  String line;
                  while ((line = br.readLine()) != null) {
                      if (line.matches("^(\\[[0-9]*\\])$")) {
                          User user = new User(null);
                          user.id = line.replace("[", "").replace("]", "");
                          user.password = br.readLine().split("-separator-")[1];
                          user.username = br.readLine().split("-separator-")[1];
                          users.add(user);
                      }
                  }
              }
              catch (IOException ex) {
                  CustomLog.error(ex.getMessage());
              }
          }
          return users;
      }
  
      public static void saveUser(User user) {
          File f = new File("users.dat");
          String content = "";
          String newLine = System.getProperty("line.separator");
          boolean found = false;
          if (f.isFile() &amp;&amp; f.canRead()) {
              try (BufferedReader br = new BufferedReader(new FileReader(f))) {
                  String line;
                  while ((line = br.readLine()) != null) {
                      if (line.equals("[" + user.id + "]") &amp;&amp; br.readLine().equals(user.password)) {
                          found = true;
                          content += "[" + user.id + "]" + newLine;
                          content += "password-separator-" + user.password + newLine;
                          content += "username-separator-" + user.username + newLine;
                          br.readLine();
                      }
                      else {
                          content += line + newLine;
                      }
                  }
              }
              catch (IOException ex) {
                  CustomLog.error(ex.getMessage());
              }
          }
          if (!found) {
              content += "[" + user.id + "]" + newLine;
              content += "password-separator-" + user.password + newLine;
              content += "username-separator-" + user.username + newLine;
          }
          try (BufferedWriter writer = new BufferedWriter(new FileWriter(f))) {
              writer.write(content);
              writer.close();
          }
          catch (FileNotFoundException | UnsupportedEncodingException ex) {
              CustomLog.error(ex.getMessage());
          }
          catch (IOException ex) {
              CustomLog.error(ex.getMessage());
          }
      }
  }
  </code></pre>