title: Multithread: Dynamically object locking based on resource in hava
body: <p>Need threads expert eyes here...</p>
  
  <p>I am on a poc app where i am uploading files on FTP server</p>
  
  <p>In FTP server have multiple folders. Based on input response I reads files from the folder and move to another folder</p>
  
  <p>The app can access by multiple threads at a time.</p>
  
  <p>So the problem was this:</p>
  
  <p>Suppose FTP have a folders Folder_A and A_A_FOLDER
  Now Folder_A have 10 files.
  a thread came and read 10 files from FTP and start some calculation on it,
  it calculated one by one and then move to  A_A_FOLDER
  it was middle in the process (let suppose it successfully moved 5 files from Folder_A to A_A_FOLDER)
  then another thread came and it picks remaining 5 files because they were underprocessed by thread 1, so thread 2 also start processing those 5 files</p>
  
  <p>So duplicate files problem here</p>
  
  <pre><code>void m1(String folderName) {
  // FTP related code
  }
  </code></pre>
  
  <p>I have solved this problem by using <strong>synchronized</strong> keyword </p>
  
  <p>Now every thing in sync and all processing working fine</p>
  
  <pre><code>synchronized void m1(String folderName) {
  // code
  }
  </code></pre>
  
  <p><strong>folderName</strong> decide which folder need to process</p>
  
  <p>Now I have started facing performance issue</p>
  
  <p>because the method is synchronized so all thread going to wait until processing thread not completed its task.</p>
  
  <p>I can improve this by following steps:</p>
  
  <p>(Before going to a solution here is the some story to much dig on the problem)</p>
  
  <p>As I have mentioned  folderName parameter of m1 method decide which folder will process, 
  So suppose I have 4 folders (A, B, A_T, B_T) in Ftp server, 2 folders are those where data need to read from (A and B),
  And 2 folder are those where data will move (A_T and B_T)</p>
  
  <p>A_T and B_T is not the concern here because they are unique for each folder A and B
  So if the method will read from A then it will move it to A_T same for B (move to B_T)</p>
  
  <p>Now:</p>
  
  <p>Suppose 4 thread comes to m1 method, 3 threads for folder A and 1 for folder B
  if somehow method synchronized request based on fileName parameter so I can improve the performance, means 1 thread will work on A another 2 threading will block because fileName is same for them so they will wait until first thread not completed it task where thread 4 will parallel work without any locking process because it's file name is different</p>
  
  <p>So how can I achieve this(synchronized on fileName) in code level? </p>
  
  <p>Note: i know i can break this logic using static locking list for resource and then the locks  fileName resource 
  e.g:</p>
  
  <pre><code>private final Object A = new Object();
  private final Object B = new Object();
  </code></pre>
  
  <p>but the problem with this approach is folder can be dynamically added, so I can't go with this.</p>
  
  <p>Need your help guys. </p>