Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Not able understand tryLock method while executing program

I was learning Java concurrency and trying ReentrantLock class methods. While trying tryLock() method and running sample program, it’s not working as it should. By definition if lock is available, then tryLock() returns true, thread will get lock & if not then it return false.

So in my code I have two threads if t1 got lock, then it will perform if part operations and t2 will perform else operations. Am I missing something? please help.

Code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import java.util.concurrent.locks.ReentrantLock;

class ReentrantLockMethods extends Thread{ 
    ReentrantLock l = new ReentrantLock();  
    
    public ReentrantLockMethods(String name) {
        super(name);
    }   

    public void run() {     
        if(l.tryLock()) {           
            System.out.println(Thread.currentThread().getName()+"....got Lock and performing safe operations");
            try {
                Thread.sleep(5000);
            }
            catch(InterruptedException e) {}
            l.unlock();         
        }
        else {
            System.out.println(Thread.currentThread().getName()+ "....unable to get Lock hence, performing alternate operations");
        }
    }
}
public class ReentrantLockDemo1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ReentrantLockMethods t1 =  new ReentrantLockMethods("First Thread");
        ReentrantLockMethods t2 =  new ReentrantLockMethods("Second Thread");       
        t1.start();
        t2.start();
    }

}

Actual Output:

First Thread....got Lock and performing safe operations
Second Thread....got Lock and performing safe operations

Expected Output:

First Thread....got Lock and performing safe operations
Second Thread....unable to get Lock hence, performing alternate operations

or

Second Thread....got Lock and performing safe operations
First Thread....unable to get Lock hence, performing alternate operations

>Solution :

In your implementation of ReentrantLockMethods, each instance has its own lock, so there is no contention, and both can acquire their lock. If you want to observe the desired behaviour, you need to ensure both threads use the same lock.

As an aside, generally you should not extend Thread, but instead implement Runnable.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading