Hi,

 

I am trying to understand the internal working of ReentrantLock in Java.

I have created an example like:-

 

package com.thread.trylock;

 

import java.util.concurrent.TimeUnit;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

 

public class ReentrantLockingDemo {

 

    final Lock lock = new ReentrantLock();

 

    public static void main(final String... args) {

        new  ReentrantLockingDemo().go();

    }

 

    private void go() {

      

        Runnable run1 = newRunable();

        Thread t1 = new Thread(run1, "Thread1");

        System.out.println(run1.hashCode());

        t1.start();

      

        Runnable run2 = newRunable();

        Thread t2 = new Thread(run2, "Thread2");

        System.out.println(run2.hashCode());

        t2.start();

      

    }

 

  

    private Runnable newRunable() {

      

        return new Runnable() {

 

            @Override

            public int hashCode() {

                return super.hashCode();

            }

           

          

            @Override

            public void run() {

                do {

                    try {

 

                        if (lock.tryLock(500, TimeUnit.MILLISECONDS)) {

                            try {

 

                                System.out.println("locked thread "

                                        + Thread.currentThread().getName());

 

                                Thread.sleep(1000);

 

                            } finally {

                                lock.unlock();

                                System.out.println("unlocked locked thread "

                                        + Thread.currentThread().getName());

 

                            }

                            break;

                        } else {

                            System.out.println("unable to lock thread "

                                    + Thread.currentThread().getName()

                                    + " will re try again");

                        }

                    } catch (InterruptedException e) {

                     

                        e.printStackTrace();

                    }

                } while (true);

            }     

        };

    }

}

 

//Output something like this (which may be slightly different on your machine)

locked thread Thread2

unable to lock thread Thread1 will re try again

locked thread Thread1

unlocked locked thread Thread2

unlocked locked thread Thread1

 

Now my question is that there are 2 threads objects and 2 runnable objects and 2 threads. Each thread should be running the run method on its own stack frame for a different runnable object. If each thread is running its own run method on stack with different runnable object the output should be different.

I have seen examples where we create few threads and we pass on a shared object onto those threads and we do lock and unlock in the method for that shared object. Here the object is not shared. There are 2 runnable objects passed to 2 threads objects but runnable object are behaving as shared object.

Can you please explain as what might be causing this output? or can provide some clarification on the same.

 

Is there any  significance of defining the ReentrantLock instance in a class?

Does it take the lock on the Object inside which it is instantiated. i.e ReentrantLockingDemo has a Reentrant object so it takes lock on ReentrantLockingDemo object?

 

Thanks

FacebookTwitterLinkedin
Pin It
Joomla Tutorials for Beginners