Java Thread holdLock() method

The holdLock() method of thread class returns true if the current thread holds the monitor lock on the specified object.

Syntax

Parameter

obj: It defines the object on which to test lock ownership

Return

It returns true if and only if the current thread holds the monitor lock on the specified object. Otherwise, it returns false.

Exception

NullPointerException: This exception throws if obj is null.

Example

Test it Now

Output:

Currently executing thread is: Thread-0
Does thread holds lock? false
Does thread holds lock? true





Latest Courses