title: Division of a task to threads - multi threading
body: <p>I want to generate pairs from a given large pool of numbers. I am using two for loops and threads. My function getAllPairs() in the code generates apairs with a given array of numbers.</p>
  
  <p>I have an array of length 1000. With one thread, output time is nearly 15 sec. Now I want to use 5-6 threads and reduce this output time.I am stuck at dividing this task equally to five threads.If not threads,how to decrease the output time?</p>
  
  <p>Solution with threads is appreciated since I put a lot of time learning multithreading. I would like to implement it.</p>
  
  <pre><code>import java.util.*;
  
  
   class Pair {
   public int x, y;
   public Pair(int x, int y) {
    this.x = x;
    this.y = y;
   }
    @Override
      public String toString(){
          return " ( " + x + " ," + y + " ) " ;
      }
  }
  
  
  class selectPairs{
   private int[] array;
   private List&lt;Pair&gt; totalPairs ;
  
   public selectPairs(int[] arr){
       array = arr;
   }
   //set Method
   public void settotalPairs(List&lt;Pair&gt; pieces){
       totalPairs = pieces;
   }
   //get Method
   public List&lt;Pair&gt; gettotalPairs(){
       return totalPairs;
   }
  
   // Method to generate pairs
   public List&lt;Pair&gt; getAllPairs() {
    List&lt;Pair&gt; pairs = new ArrayList&lt;Pair&gt;();
    int total = array.length;
    for(int i=0; i &lt; total; i++) {
    int num1 = array[i];
    for(int j=i+1; j &lt; total; j++) {
     int num2 = array[j];
     pairs.add(new Pair(num1,num2));
     }
    }
    return pairs;
   } 
  }
  
  
  // Thread class
  class ThreadPairs extends Thread {
     private Thread t;
     selectPairs SP;
  
     ThreadPairs(selectPairs sp){
  
          SP = sp;
     }
     public void run() {
        synchronized(SP) {
         List&lt;Pair&gt; PAIRS = SP.getAllPairs();
         SP.settotalPairs(PAIRS);
       }
     }
  }
  
   public class TestThread {
     public static void main(String args[]) {
  
         int[] a = new int[1000]; 
         for (int i = 0; i &lt; a.length; i++) { 
          a[i] = i ;
          }
  
        selectPairs ob = new selectPairs(a);
        ThreadPairs T = new ThreadPairs(  ob );
        T.start();
  
        while (true) {
          try {
           T.join(); 
           break;
          }
          catch(Exception e){
          }
        }
  
        List&lt;Pair&gt; Total = new ArrayList&lt;Pair&gt;() ;
        List&lt;Pair&gt; Temp1 = ob.gettotalPairs();
        Total.addAll(Temp1);
        System.out.println(Total);
     }
  }
  </code></pre>