When you use the
synchronized keyword to protect a block of code, you must pass an object reference as a parameter. Normally, you will use the
this keyword to reference the object that executes the method, but you can use other object references. Normally, these objects will be created exclusively with this purpose. For example, if you have two independent attributes in a class shared by multiple threads, you must synchronize the access to each variable, but there is no problem if there is one thread accessing one of the attributes and another thread accessing the other at the same time.
In this recipe, you will learn how to resolve this situation's programming with an example that simulates ...