I've been reading Java's source code regarding Executors services. Upon inspecting ThreadPoolExecutor class I've found this code for shutdown:
public void shutdown() {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
checkShutdownAccess();
advanceRunState(SHUTDOWN);
interruptIdleWorkers();
onShutdown(); // hook for ScheduledThreadPoolExecutor
} finally {
mainLock.unlock();
}
tryTerminate();
}
And I don't understand what's the point of this lock being local to the method. If my understanding is right new lock is created for every call to this function which kinda makes this lock redundant (as in thread calling always passes the locking) and it is not visible anywhere outside methods scope.
This coding pattern repeats throughout the class, with declaring local final lock, locking and then unlocking it.
EDIT:
- fixed code misreading nonsense