The Java Lock interface represents a concurrent lock which can make sure only one thread at a time can lock the lock, perform its critical logic atomically, and unlock the lock again. A Java Lock is similar to a Java Synchronized block, but is more flexible and has more features.
A Java Lock can be reentrant, meaning the same thread can lock the Lock more than one time. The Lock must then be unlocked the same number of times before it is fully unlocked for other threads.
A Java Lock can guarantee fairness among threads waiting to lock the Lock, meaning the threads will be guaranteed to be allowed to lock the Lock in the same sequence the threads called the Lock.lock() method. This prevents the situation of starvation, where a waiting thread is never allowed to lock the Lock because other threads keep "passing" it in the waiting queue.
Chapters:
0:00 Java Lock and ReentrantLock introduction
2:02 Two concurrent Counter class examples
3:38 Lock and unlock the Lock inside a try-finally clause
5:00 Lock reentrance
7:03 Lock reentrance use case
10:24 Lock fairness
14:40 Lock examples
19:25 Lock.lockInterruptibly()
20:42 Lock.tryLock()
24:04 ReentrantLock methods
26:07 Java Lock vs. synchronized blocks - differences and similarities.
Java Lock Tutorial - text:
http://tutorials.jenkov.com/java-util-concurrent/lock.html
Java Synchronized Blocks - text / video:
http://tutorials.jenkov.com/java-concurrency/synchronized.html
https://www.youtube.com/watch?v=eKWjfZ-TUdo&list=PLL8woMHwr36EDxjUoCzboZjedsnhLP1j4&index=5
Reentrance Lockout - text:
http://tutorials.jenkov.com/java-concurrency/reentrance-lockout.html
Starvation and Fairness - text:
/java-concurrency/starvation-and-fairness.html
Java Concurrency tutorial - text / video playlist:
http://tutorials.jenkov.com/java-concurrency/index.html
https://www.youtube.com/playlist?list=PLL8woMHwr36EDxjUoCzboZjedsnhLP1j4