title: the multithread not working with me
body: <p>I have to write program that finds the sum of a 2D array of int,
  I coded every thing as I know and there is no syntax error but when I use someways to check my code the thread is not working at all but sometimes work some of thread not all of them 
  I put the number 1 to check the summation
  and I put lock to make sure not two of thread in same method of summation only for make sure 
  and the n for see how much time it's join the add method</p>
  
  <pre><code>public class extend extends Thread {
  
      int a, b;
      private static int sum = 0;
      static int n;
      boolean lock;
      int[][] arr;
  
      public extend() {
          arr = new int[45][45];
          for (int i = 0; i &lt; 45; i++) {
              for (int j = 0; j &lt; 45; j++)
                  arr[i][j] = 1;
          }
          n = 0;
          lock = false;
  
      }
  
  
      public extend(int a, int b) {
          arr = new int[45][45];
          for (int i = 0; i &lt; 45; i++) {
              for (int j = 0; j &lt; 45; j++)
                  arr[i][j] = 1;
          }
          n = 0;
          lock = false;
          this.a = a;
          this.b = b;
      }
  
  
      public void run() {
          add(a, b);
      }
  
  
      public void add(int st, int e) {
  
          n++;
          while (lock) ;
          lock = true;
          int sums = 0;
          synchronized (this) {
  
              for (int i = st; i &lt; e; i++) {
                  for (int j = 0; j &lt; 45; j++) {
                      sums += arr[i][j];
                  }
              }
          }
          sum = sums;
          lock = false;
      }
  
  
      public int getSum() {
          return sum;
      }
  
  
      public static void main(String[] args) {
          long ss = System.currentTimeMillis();
          Thread t1 = new Thread(new extend(0, 9));
          Thread t2 = new Thread(new extend(9, 18));
          Thread t3 = new Thread(new extend(18, 27));
          Thread t4 = new Thread(new extend(27, 36));
          Thread t5 = new Thread(new extend(36, 45));
  
          t1.start();
          t2.start();
          t3.start();
          t4.start();
          t5.start();
  
          long se = System.currentTimeMillis();
  
          System.out.println("The sum for 45*45 array is: " + sum);
          System.out.println("time start;" + (se - ss));
          System.out.print(n);
  
      }
  
  }
  </code></pre>