Every object in Java has a corresponding lock and that lock is used in multithreading enviornment to ensure the desirable access to the methods or the block of code. When there are more than one thread working on the non-static methods (or the block of code in those methods.) than the lock taken on the object (the instance of some class) is taken by the thread and no other thread with the same instance of object can access those methods if they are synchronized. That means by synchronizing the method, you will ensure that any threads trying to run that method using the same instance will be prevented from simultaneous access. But thread working with a different instance will not be affected, because it's acquiring a lock on the other instance.
Bring out your dead! Or a tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java