title: Java IllegalMonitorStateException on tryLock
body: <p>I was wondering if someone could help me. I'm new to concurrent programming and I've got the following code which at times is giving me an IllegalMonitorStateException as a Thread that does not currently own the lock is trying to unlock.</p>
  
  <p>I was told on the OTN forum that to prevent this and deadlock from occurring that I should make sure that the lowest indexes are locked first but I can't figure out how to do this. could one of you please point me in the right direction.</p>
  
  <p>Many thanks.</p>
  
  <pre><code>         import java.util.concurrent.locks.*;
           class ShuffleBase {
  
           ReentrantLock [] locker;
           int [] data;
           int x;
           ReentrantLock lock1;
           ReentrantLock lock2;
  
           public ShuffleBase(int [] d){
           x=0;
          data=d;
          locker = new ReentrantLock[d.length];
          while (x!=data.length)
          {
           locker[x]=new ReentrantLock();   
            x++;
          }
          }
  
  
       /*
        *  Boolean method to test if both indexes are locked
        *  Returning their status for use in the method swap
        *  If locked the swap happens else the locks are 
        *  released
        */
        public boolean lockedIndex(int a, int b){
  
        Boolean lockA = false;
  
        Boolean lockB = false;
  
        try{
  
        lockA = lock1.tryLock();
  
        lockB = lock2.tryLock(); 
         }finally{
  
            if (!(lockA &amp;&amp; lockB))
        {
  
         if(lockA){
           lock1.unlock();       
          }
         if (lockB){
          lock2.unlock();
           }  
             }// end of IF ! lockA &amp; lockB
  
        } // End of finally
        return lockA &amp;&amp; lockB;
      }// End of lockedIndex
  
  
       public void swap(int a, int b){
  
        int temp;
  
        lock1 = locker[a];
        lock2=locker[b];
  
  
  
        //If a &amp; b aren't the same index swap
         if(a!=b)
          {   
          if(lockedIndex(a,b))
          {
           try{
            temp=data[b];
  
           data[b]=data[a];
  
           data[a]=temp;
  
            }finally{
              lock1.unlock(); 
                         lock2.unlock();
  
            }
           }
           else{System.out.println("Couldn't lock");}   
  
          }
         else{return;}
  
      }//EOF Method
  
      public void display(){
      System.out.println("The array when swapped");
      for(int j=0; j&lt;data.length;j++)
      {
       System.out.print(data[j]+", ");
      }
      }// End of Display
  
  
       }// EOC
  </code></pre>
  
  <hr>
  
  <p>Thank you all for your answers, yes I am doing this as part of a self learning/homework.
  I've changed the code to the following as I believe that this is what Rodion suggested, I hope I interpreted your suggestion correctly. Mtraut I've also made the changes you suggested and I can't believe that I made these mistakes :-) Thanks for spotting them.</p>
  
  <pre><code>import java.util.concurrent.locks.*;
  class ShuffleBase {
  
  ReentrantLock [] locker;
  int [] data;
  int x=0;
  
  
  public ShuffleBase(int [] d){
    data=d;
    locker = new ReentrantLock[d.length];
    while (x!=data.length)
     {
       locker[x]=new ReentrantLock();   
        x++;
     }
  }
  
  
   /*
    * Boolean method to test if both indexes are locked
    *  Returning their status for use in the method swap
    *  If locked the swap happens else the locks are 
    *  released
    */
    private boolean lockedIndex(int a, int b){
  
     Boolean lockA = false;
  
     Boolean lockB = false;
  
      try{
  
          lockA = locker[a].tryLock();
  
          lockB = locker[b].tryLock(); 
          }finally{
  
           if (!(lockA &amp;&amp; lockB))
               {
  
                   if(lockA){
                   locker[a].unlock();       
                   }
  
                   if (lockB){
                     locker[b].unlock();
                   }  
               }// end of IF ! lockA &amp; lockB
  
         } // End of finally
             return lockA &amp;&amp; lockB;
      }// End of lockedIndex
  
  
  public void swap(int a, int b){
  
   int temp; 
  
   //If a &amp; b aren't the same index swap
     if(a!=b)
            {   
              if(lockedIndex(a,b))
                {
                   try{
                        temp=data[b];
                        data[b]=data[a];
                        data[a]=temp;
                       }finally{
                                locker[a].unlock(); 
                                locker[b].unlock();
                                }
                 }
               else{System.out.println("Couldn't lock");}   
             }
             else{System.out.println(return;}
  
         }//EOF Method
  
     public void display(){
         System.out.println("The array when swapped");
         for(int j=0; j&lt;data.length;j++)
             {
                System.out.print(data[j]+", ");
             }
         }// End of Display
  
    }// EOC
  </code></pre>
  
  <p>Thanks again for all of your answers and apologies for my late reply.</p>