Javatpoint Logo
Javatpoint Logo

Java ReentrantLock getHoldCount() Method

The getHoldCount() method of ReentrantLock class is used to get the number of holds on this lock by the current thread.

Syntax

Parameter

No parameter is passed.

Returns

the number of holds on this lock by the current thread, or zero if the current thread does not hold this lock

Throws

No Exception is thrown.

Example 1

Test it Now

Output:

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 2

Test it Now

Output:

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





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA