title: Java Parallel File Processing
body: <p>I have following code:</p>
  
  <pre><code>import java.io.*;
  import java.util.concurrent.* ;
  public class Example{
  public static void main(String args[]) {
      try {
          FileOutputStream fos = new FileOutputStream("1.dat");
          DataOutputStream dos = new DataOutputStream(fos);
  
          for (int i = 0; i &lt; 200000; i++) {
              dos.writeInt(i);
          }
          dos.close();                                                         // Two sample files created
  
          FileOutputStream fos1 = new FileOutputStream("2.dat");
          DataOutputStream dos1 = new DataOutputStream(fos1);
  
          for (int i = 200000; i &lt; 400000; i++) {
              dos1.writeInt(i);
          }
          dos1.close();
  
          Exampless.createArray(200000); //Create a shared array
          Exampless ex1 = new Exampless("1.dat");
          Exampless ex2 = new Exampless("2.dat");
          ExecutorService executor = Executors.newFixedThreadPool(2); //Exexuted parallaly to cont number of matches in two file
          long startTime = System.nanoTime();
          long endTime;
          Future&lt;Integer&gt; future1 = executor.submit(ex1);
          Future&lt;Integer&gt; future2 = executor.submit(ex2);
          int count1 = future1.get();
          int count2 = future2.get();
          endTime = System.nanoTime();
          long duration = endTime - startTime;
          System.out.println("duration with threads:"+duration);
          executor.shutdown();
          System.out.println("Matches: " + (count1 + count2));
  
          startTime = System.nanoTime();
          ex1.call();
          ex2.call();
          endTime = System.nanoTime();
          duration = endTime - startTime;
          System.out.println("duration without threads:"+duration);
  
      } catch (Exception e) {
          System.err.println("Error: " + e.getMessage());
      }
  }
  }
  
  class Exampless implements Callable {
  
  public static int[] arr = new int[20000];
  public String _name;
  
  public Exampless(String name) {
      this._name = name;
  }
  
  static void createArray(int z) {
      for (int i = z; i &lt; z + 20000; i++) { //shared array
          arr[i - z] = i;
      }
  }
  
  public Object call() {
      try {
          int cnt = 0;
          FileInputStream fin = new FileInputStream(_name);
          DataInputStream din = new DataInputStream(fin);      // read file and calculate number of matches
          for (int i = 0; i &lt; 20000; i++) {
              int c = din.readInt();
              if (c == arr[i]) {
                  cnt++;
              }
          }
          return cnt ;
      } catch (Exception e) {
          System.err.println("Error: " + e.getMessage());
      }
      return -1 ;
  }
  
  }
  </code></pre>
  
  <p>Where I am trying to count number of matches in an array with two files. Now, though I am running it on two threads, code is not doing well because: </p>
  
  <p>(running it on single thread, file 1 + file 2 reading time) &lt; (file 1 || file 2 reading time in multiple thread). </p>
  
  <p>Can anyone help me how to solve this (I have 2 core CPU and file size is approx. 1.5 GB).</p>