Java ReentrantLock lock() MethodThe 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. SyntaxParameterNo parameter is passed. ReturnsDoes not have a return type. ThrowsNo Exception is thrown. Example 1Test it NowOutput: after sleep(1500) Is locked - true task name - Job1 work done Example 2Test it NowOutput: 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
|