Results 1 to 3 of 3
  1. #1
    IrishG is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default Need help with TryLock()

    Hi all,

    This is my first post so I hope you will bear with me, and that this is the correct forum :-)

    I have a problem with the following code, in that at times it throws an IllegalMonitiorStateException as show directly below and other times it works fine.

    Java Code:
    Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
    	at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(Unknown Source)
    	at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(Unknown Source)
    	at java.util.concurrent.locks.ReentrantLock.unlock(Unknown Source)
    	at ShuffleBase.swap(ShuffleBase.java:45)
    	at Shuffle.run(Shuffle.java:18)
    	at java.lang.Thread.run(Unknown Source)
    I believe from what I've read online that this is caused by a thread who does not own the lock trying to release the lock. I was advised on the OTN site to try and ensure that the lowest indexes are locked first, I've tried all that I could think of but to no joy I was hoping that someone here may be able to help me with this.

    Many thanks IrishG.

    My current code is as follows


    Class that tries to swap the elements of the array using tryLock
    Java 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 there status for use in the method swap
    	 */
    	public boolean lockedIndex(int a, int b){
    		
    		  Boolean lockA = false;
    		  
    		  Boolean lockB = false;
    		  
    			  try{
    				  
    				  lockA = lock1.tryLock();
    				  
    				 lockB = lock2.tryLock();
    				  
    			  }finally{
    				  
    				  if (!(lockA && lockB))
    				  {
    					  
    					  if(lockA){
    						  	lock1.unlock();
    						  	
    						  }
    					  if (lockB){
    						  lock2.unlock();
    						  
    					  }
    					  
    				  }// end of IF ! lockA & lockB
    			  }// End of finally
    		  
    		return lockA && lockB;
    	}
    	
    	
    	public void swap(int a, int b){
    		
    		int temp;
    	
    		lock1 = locker[a];
    		lock2=locker[b];
    		
    		 //If a & b aren't the same index swap
    		   if(a!=b)
    		   {			
    			if(lockedIndex(a,b))
    			  {   
    			     try{
    					 		
    				 // For Testing
                                    //System.out.println("A is now "+a + " B is now "+b);
    					 			 
                                         temp=data[b];
    						   
    				     data[b]=data[a];
    						   
    				     data[a]=temp;
    						 
    				}finally{
    					 			      
    					   lock1.unlock(); 
    					   lock2.unlock();
    					                  
    			                     }
    		           }
    			   else{System.out.println("Couldn't lock");}	  
    					
    		   }
    			   
    		}//EOF Method
    	
    	public void display(){
    		System.out.println("The array when swapped");
    		for(int j=0; j<data.length;j++)
    		{
    			System.out.print(data[j]+", ");
    		}
    		
    		
    	}
    		
    
    }


    Runnable class that calls the ShuffleBase class.

    Java Code:
     class Shuffle implements Runnable{
    	
    	ShuffleBase b;
    	public Shuffle(ShuffleBase c){
    		b=c;
    	}
    	
    	
    	public void run(){
    		int k=0;
    		int z = b.data.length;
    		
    	 while(k!=b.data.length)
    		{
    			int x = (int)(0+z*Math.random());
    			int y =(int)(0+z*Math.random());
    			try{
    				Thread.sleep(100);
    			}catch(InterruptedException e){}
    			 b.swap(x,y);
    			 k++;
    		}
    	}
    
    	
    }
    Last edited by IrishG; 12-28-2010 at 07:50 PM.

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    6

    Default

    Please use code tags to show your code, else its very tough to read.
    Swastik

  3. #3
    IrishG is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    Sorry but I believe that I did wrap my code in the required tags, as supplied by the editor.

    Is it that my code is quite long, if so I've placed a couple of extra spaces and between them to try and make the code more clear.

    Sorry if any offence caused.
    Last edited by IrishG; 12-28-2010 at 07:47 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •