Multithreading - Locks
Traditional multithreaded programming comes with a single way to get the lock i.e. synchronized keyword. All the threads wait until a thread releases the lock i.e. leaves the synchronized block. With the new concurrent api, locks were introduced to give the following facilities : 1. You can try for acquiring a lock, if you get acquire it, if not, you do something else/wait as you wish. 2. You can specify for how much time you want to wait to acquire a lock. This is one of the ways to avoid deadlocks, since thread will not go in a wait state forever. 3. If multiple threads are waiting for a lock, we now have an option to define if the thread waiting for the longest time gets the lock. 4. synchronized keyword was applicable only at method level or block level, but now we have an option to use locks across multiple methods. The locks functionality is present in the java.util.concurrent.locks package starting JDK 1.5 The top interface here is Lock, which defines the basic mecha