title: File Handling issue in Multi threaded Environment
body: <p>I am facing a strand kind of issue in multi threaded environment.
   Though this code was pretty old and was working since long time.</p>
  
  <p>One of the person complained that they are facing issue like. Even
   though the file created by one thread exist , another thread saying no
   file exist.</p>
  
  <p>I providing a sample method where the problem is coming..</p>
  
  <pre><code>/**
    * Creates a temporary directory. Will be deleted when the program closed if it
    * is empty!
    *
    * @return the temporary directory
    * @throws com.osm.exception.WMException if there is a problem
    */
  public static File createTempDir() throws WMException {
      synchronized (pdm.semaphore) {
          try {
              final File parent = WMSession.getWMSession().getRootTempDir();
              if (!parent.exists()) {
                  throw new IllegalStateException(parent + " does not exist"); //frozen
              }
  
              final File tmpDirectory = File.createTempFile("WMTempDir", "", parent); //frozen
              tmpDirectory.delete();
              tmpDirectory.mkdirs();
              logFileCreated(tmpDirectory);
              return tmpDirectory;
          } catch (final IOException ioe) {
              throw new WMException(ioe);
          }
      }
  }
  </code></pre>
  
  <p>This code is being called from another method code as below.</p>
  
  <pre><code>void copy_from_db2_using_temp_dir(String phys_name, int storage_type, int store_date, int file_flag,
          String directory, String file_name) throws WMException {
          final File destDir = new File(directory);
  
          if (!destDir.exists()) {
              // no conflict possible since destination directory does not yet exist.
              pdm.copy_from_db2(phys_name, storage_type, store_date, file_flag, directory, file_name);
  
              return;
          }
  
          final File tmpDir = WMFile.createTempDir();
          final File tmpDestFile = new File(tmpDir, file_name);
          final File destFile = new File(directory, file_name);
  
          try {
              final boolean destFileExistsFlag = destFile.exists();
  
              if (destFileExistsFlag &amp;&amp; (file_flag != DEL_OLD)) {
                  final String msg = pdm.fix_mesg(pdm.catgets("data_mgr", 266, "*** ERROR: Cannot overwrite file '{1}'"),
                          destFile.getAbsolutePath());
                  throw new WMException(msg);
              }
  
              pdm.copy_from_db2(phys_name, storage_type, store_date, file_flag, tmpDir.getAbsolutePath(), file_name);
  
              if (tmpDestFile.isFile() &amp;&amp; destFile.isDirectory()) {
                  final String msg = pdm.fix_mesg(pdm.catgets("data_mgr", 269, "*** ERROR: Could not remove file '{1}'"),
                          destFile.getAbsolutePath());
                  throw new WMException(msg);
              }
  
              moveFiles(tmpDestFile, destFile, (file_flag == DEL_OLD));
          } finally {
              deleteTempDir(tmpDir);
          }
      }
  </code></pre>
  
  <p>The another thread/process always getting the condition
  <code>!parent.exists()</code> true. Which is incorrect as it must get the parent
  file.</p>
  
  <p>Need suggestion input or any logging that will helpful to know if the
  invocation has some issue or some issue in the code.</p>
  
  <p>I got something on StackOverflow but not sure if that is relevant
  here.
  <a href="https://stackoverflow.com/questions/34644187/why-is-file-exists-behaving-flakily-in-multithreaded-environment">File.exists() issue in multi threaded env </a></p>