The general form of synchronized block is
void printTable(int n)
Here obj means object of table class or object of any class.
If we write 'this' in place of obj ,then an object of table class canít access the synchronized block from two different threads.
1)Is it possible to write name of object other than table class in place of obj?
2)If write other objectís name other than table class in place of obj,what it means?
3)Then which object canít access the synchronized block simultaneously?
1. It is possible to put any object in a synchronized block.
When a thread enters a synchronization block, it is said to have a lock on that object. That lock is released once the block is left.
What happens is if a thread has a lock on an object, and another thread tries to lock that object by entering a synchronization block, it will wait until the lock is free.
The important things to note is that the synchronization blocks can be in different locations, but if they are on the same object, then they will block each other on different threads. Also note that a thread with a lock on an object doesn't prevent it from entering other synchronized blocks on the same object. Also, a thread needs to have a lock on an object in order to use wait() or notify() on the object. When a thread uses wait, it releases the lock on that object, then reacquires it when it continues.