title: While Parallelizing QuickSort in Java, Threads never returns back at join(). Why?
body: <p>I am having simple code of paralellizing QuickSort algorithm in Java, in run method I everytime create two seperate new threads for parallelizing processing of Array elements. But as it encounters join() statements for both created threads, threads never backs and halts on joins(), seems join() never releases them.</p>
  
  <p>Below is the code.</p>
  
  <pre><code>class StartJoinQuickSort implements Runnable 
  {
      private int m_Low, m_High;
      private int[] m_Array = null;
  
      private final static int NR_OF_VALUES = 10; // TOTAL_NO_VALUES
      private int PivotElement;   
      private static Random m_random = new Random( );
  
  
      public StartJoinQuickSort(int[] a_Array,int a_Low,int a_High)
      {
          this.m_Array = a_Array;
          this.m_Low = a_Low;
          this.m_High = a_High;
      }
      private void SwapArrayElements(int a_i,int a_j)
      {
          int temp = this.m_Array[a_i];
          this.m_Array[a_i] = this.m_Array[a_j];
          this.m_Array[a_j] = temp;
  
      }// end of SwapArrayElements
  
      private static int nextRandomFunctionValue(int aStart, int aEnd)
      {
          if ( aStart &gt; aEnd )
          {
             throw new IllegalArgumentException("Start cannot exceed End.");
          }
  
          //get the range, casting to long to avoid overflow problems
          long range = (long)aEnd - (long)aStart + 1;
  
          // compute a fraction of the range, 0 &lt;= frac &lt; range
  
          long fraction = (long)(range * m_random.nextDouble());
          int randomNumber =  (int)(fraction + aStart);    
          return randomNumber;
  
      }// end of nextRandomFunctionValue
  
      private static int[] GetArrayWithRandomValues()
      {
          int[] ArrayToBePopulatedWithRandomValues  = new int[NR_OF_VALUES];
          for(int index =0; index&lt;NR_OF_VALUES;index++)
          {
              int RandomValue = StartJoinQuickSort.nextRandomFunctionValue(0,NR_OF_VALUES);
              ArrayToBePopulatedWithRandomValues[index] =  RandomValue;
  
          }//end of for
  
          return ArrayToBePopulatedWithRandomValues;
  
      }//end of GetArrayWithRandomValues
      private int middleIndex(int left, int right) 
      {
          return left + (right - left) / 2;
      }
      public int Partition(int a_Start,int a_end)
      {
      //  System.out.println("Partition ..thId : " + Thread.currentThread().getId());
          int pivotIndex = 0;
  
          int i = a_Start;
          int j = a_end;
  
          try
          {
              pivotIndex = middleIndex(a_Start , a_end);
              this.PivotElement = this.m_Array[pivotIndex];
  
              do
              {
                  while(this.m_Array[i] &lt; PivotElement )
                      i++;
  
                  if(j&gt;0)
                  {
                      try
                      {
                          while( this.m_Array[j] &gt; PivotElement )
                              j--;
                      }
                      catch(Exception ex){System.out.println(" j : " + j);}
  
                  }//end of if
  
                  if(i&lt;=j)
                  {
                      SwapArrayElements(i,j);
  
                  //  System.out.println("Swap .." + + Thread.currentThread().getId());
  
                      i++;
                      j--;
  
                  }//end of if
  
              }while(i&lt;=j);
          }
          catch(Exception except)
          {
              System.out.println("exception in Partition " + except);
          }
          return j;
      }
  
      public void run()  
      {
  
          //System.out.println("in run..");
  
  
          //System.out.println("after PARTITION");
  
          StartJoinQuickSort oStartQuickSort_1 = null;
          StartJoinQuickSort oStartQuickSort_2 = null;
  
  
  
          if(this.m_Low &lt; this.m_High )
          {
              int Index = Partition(this.m_Low,this.m_High);
  
  
  
              Thread thPart_1 = new Thread ( new StartJoinQuickSort( this.m_Array,this.m_Low,Index ) );
              Thread thPart_2 = new Thread ( new StartJoinQuickSort( this.m_Array,Index + 1,this.m_High ) );
  
              thPart_1.start();   thPart_2.start();
  
  
          //}//end of if
          //if( Index + 1 &lt; this.m_High)
          //{         
              try
              {           
                      thPart_1.join(); thPart_2.join();
  
              }catch (InterruptedException e) { e.printStackTrace();}         
          }
      }//end of run
  </code></pre>
  
  <p>Regards
  Usman</p>