Java ReentrantLock getHoldCount() MethodThe getHoldCount() method of ReentrantLock class is used to get the number of holds on this lock by the current thread. SyntaxParameterNo parameter is passed. Returnsthe number of holds on this lock by the current thread, or zero if the current thread does not hold this lock ThrowsNo Exception is thrown. Example 1Test it NowOutput: task name is- Job1 outer lock at 02:54:52 outer work task name is- Job1 inner lock at 02:54:56 inner work Lock Hold Count is - 2 Lock Hold Count - 2 task name - Job1 work done Example 2Test it NowOutput: task name - Job2 waiting for lock task name - Job1 outer lock at 02:56:23 outer work task name - Job2 waiting for lock task name - Job1 inner lock acquired at 02:56:24 Doing inner work Lock Hold Count - 2 task name - Job2 waiting for lock task name - Job2 waiting for lock task name - Job1 releasing lock (inner lock) Lock Hold Count - 1 task name - Job1 work done task name - Job1 releasing lock(outer lock) Lock Hold Count - 0 task name - Job2 outer lock at 02:56:27 outer work task name - Job2 inner lock acquired at 02:56:28 Doing inner work Lock Hold Count - 2 task name - Job2 releasing lock (inner lock) Lock Hold Count - 1 task name - Job2 work done task name - Job2 releasing lock(outer lock) Lock Hold Count - 0
Next TopicJava ReentrantLock
|