title: Java-Threads ArrayIndexOutOfBoundsException
body: <p>I have made a program (for teaching myself purposes) with threads, which is calculating multiplication of elements of an array , using 2 threads. But I get for both threads an <strong>ArrayIndexOutOfBoundsException</strong>. The error looks like this: </p>
  
  <pre><code>Exception in thread "Thread-1" Exception in thread "Thread-0" Executing multiplying of last2elements
            java.lang.ArrayIndexOutOfBoundsException: 1
            at FirstPart.run(FirstPart.java:12)
            java.lang.ArrayIndexOutOfBoundsException: 1
            at SecondPart.run(SecondPart.java:10)
  </code></pre>
  
  <p>Java Code :</p>
  
  <p>FirstPart.java</p>
  
  <pre><code>public class FirstPart extends Thread {
      int multiply = 1;
      static int n;
      static int[] v = new int[n];
  
      public void run() {
          System.out.println("Executing multiplying of first "+MultiplyDemo.cap1+"elements"); 
          for(int i = 1; i &lt;= MultiplyDemo.cap1; i++) {
              multiply = multiply * FirstPart.v[i];
              System.out.println("Multiplication is "+ multiply);
          }
  
      }
  }
  </code></pre>
  
  <p>SecondPart.java:</p>
  
  <pre><code>public class SecondPart extends Thread {
      int multiply = 1;
  
      public void run() {
          System.out.println("Executing multiplying of last " + (FirstPart.n - MultiplyDemo.cap1) + "elements"); 
  
          for(int i = MultiplyDemo.cap1;i &lt;= FirstPart.n; i++) {
              multiply=multiply*FirstPart.v[i];
              System.out.println("Multiplication is "+multiply);
          }
      }
  }
  </code></pre>
  
  <p>MultiplyDemo.java:</p>
  
  <pre><code>import java.util.Scanner;
  import java.util.Vector;
  
  
  public class MultiplyDemo {
  
      public static int cap1;
  
  
      public static void main(String[] args) {
          Scanner s = new Scanner(System.in);
          System.out.println("introduceti numarul de elemente din vector:");
          FirstPart.n=s.nextInt();
  
          int []v=new int[FirstPart.n];
          System.out.println("Elementele vectorului sunt");
          for(int i = 0; i &lt; v.length; i++)
              v[i]=s.nextInt();         
          cap1=FirstPart.n / 2;
  
          FirstPart thread1=new FirstPart();
          SecondPart thread2=new SecondPart();
  
          thread1.start();
          thread2.start();
  
          try { // wait for completion of all thread and then sum
              thread1.join();
              thread2.join(); //wait for completion of MathCos object 
              double z = thread1.multiply + thread2.multiply;
              System.out.println("produsul elementelor este" +z);
          } 
          catch(InterruptedException IntExp) {
          }
  
      }
  }
  </code></pre>