0

Why after acquiring lock control is going to second lock.lock() statement? Isin't it should wait indefinitely over 2nd lock statement for acquiring it?

Is it because I am acquiring lock in single thread?

   Class LockTest{
        private static final ReentrantLock lock = new ReentrantLock();  
        public static void main(String[] args){
            lock.lock();
            System.out.println(lock.isLocked());
            lock.lock();
            System.out.println(lock.isLocked());
        }
    }

Output:

true
true
sourcecode
  • 1,802
  • 2
  • 15
  • 17
  • 3
    What do you think the _Reentrant_ in `ReentrantLock` means? – Savior Apr 25 '16 at 19:10
  • Thanks @Pillar the provided link is helpful – sourcecode Apr 25 '16 at 19:13
  • `Isin't it should wait indefinitely over 2nd lock statement for acquiring it` why would you want that? – Peter Lawrey Apr 25 '16 at 19:15
  • @PeterLawrey I m recursively creating multiple thread instances in ThreadPoolExecutor to execute some function and wanted to wait until all the threads are completed before executing the shutdown over executor . So used Lock for waiting indefinitely until it is released.. – sourcecode Apr 25 '16 at 19:29

0 Answers0