How to Use Locks in Java
When writing such multi-threaded code you have to pay particular attention when accessing shared mutable variables concurrently from multiple threads. Luckily Java supports thread-synchronization since the early days via the synchronized keyword. Synchronization refers to multi-threading. A synchronized block of code can only be executed by one thread at a time. Some of the important methods are lock() to acquire the lock, unlock() to release the lock, tryLock() to wait for lock for a certain period of time, newCondition() to create the Condition etc.
What is an object's lock in Java and which object's have locks?
Object Level Locking is a mechanism used to synchronize a non-static content of code, so that only one object can hold the monitor and can access the non-static content. Developers can use this lock when two or more threads are sharing the same object of the class that implements Runnable or extends Thread. All objects and classes have locks.
Difference between class locking and object locking in Java
Class Level Locking is used to synchronize a static content of class, so that it can be used to make class level data thread safe. The only difference is that a static synchronized locks on the class instance and a non-static synchronized method locks on the object.