title: Writing to multiple files in a multithreaded way in java
body: <p>I have a application in Java in which I need to use multi-threading. 
  I have a list of <code>ID'</code>s which is primary key for tables stored in DynamoDB.</p>
  
  <p>Say, the list is :</p>
  
  <blockquote>
    <p>| ID_1 | ID_2 | ID_3 | ID_4|.......| ID_n|</p>
  </blockquote>
  
  <p>Now I want multiple threads to read these <code>ID's</code> and do the following for each ID:</p>
  
  <ol>
  <li><p>Each thread should take a ID and query DynamoDB tables (there are two dynamo DB tables for which ID is the primary key)</p></li>
  <li><p>The result of querying each Dynamo DB table should be stored in a separate file.</p></li>
  </ol>
  
  <p>Essentially, <code>Thread_1</code> should pick up a ID say <code>ID_1</code>, it should query DynamoDB tables <code>DDB_1</code> and <code>DDB_2</code>. The result of querying <code>DDB_1</code> should go in <code>File1</code> and result of <code>DDB_2</code> should go in <code>File_2</code>. This needs to be done for all the threads. Finally, when all threads have completed execution I should have two files <code>File_1</code> and <code>File_2</code> containing results of query from all the threads.</p>
  
  <p>I have come up with a solution that let all producer threads (threads which get the query results from Dynamo DB) queue the results of the query to a single consumer thread which writes to a file say <code>File_1</code>. Similarly all producer threads write to a second queue and a second consumer thread writes to <code>File_2</code>.</p>
  
  <p>Do you feel any flaw in the approach above? Is there a better way to apply multi-threading in this case? </p>