What happens in the following code? Does the synchronization work? This is an interview question.
class T
{
public static void main(String args[])
{
Object myObject = new Object();
synchronized (myObject)
{
myObject = new Object();
} // end sync
}
}
Each time you enter the synchronized block, you synchronize on a different object. Most of the time this will not do anything except confuse you, but there is a small chance that two threads will see the same object and wait.
For this reason any code analysers will give you a warning if you are synchronizing on a field which is not
final.