Javatpoint Logo
Javatpoint Logo

Java ReentrantLock lock() Method

The lock() method of ReentrantLock class hold the lock if it is not held by another thread and returns immediately, and set the lock hold count to one.

Syntax

Parameter

No parameter is passed.

Returns

Does not have a return type.

Throws

No Exception is thrown.

Example 1

Test it Now

Output:

after sleep(1500) Is locked - true
task name - Job1 work done

Example 2

Test it Now

Output:

java.lang.ThreadGroup[name=main,maxpri=10]
     java.lang.ThreadGroup[name=main,maxpri=10]
     java.lang.ThreadGroup[name=main,maxpri=10]
     java.lang.ThreadGroup[name=main,maxpri=10]
     false
     java.lang.ThreadGroup[name=main,maxpri=10]
     [Ljava.lang.StackTraceElement;@6bd19be0
 after sleep(1500) Is held by Current Thread - true
task name - Job1 work done
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