Thread synchronization is the process in which only one thread can use a particular method or object and no other thread can access the same method or object at the same time. Thread gets a lock on the object and until the thread releases the lock from that object no other thread can use the same object.
“Synchronized” keyword is used in Java to implement thread synchronization.
Synchronized keyword can be used in two ways:
1. Synchronized keyword can be used with the methods. When a method is declared synchronized then the thread gets a lock on that method and unless that thread releases the lock no other thread can use that method.
For e.g.: synchronized void deposit()
{
}
2. Synchronized keyword can also be used with a block of statements. We can apply synchronized to a set of statements rather than the whole methods.
Eg: synchronized (object)
{
}