when I am synchronizing on some object,
see a snippet of code,
Object1 obj1;
Object2 obj2;
synchronized(obj1)
{
// Modifying obj1 as well obj2
}
My doubt is, threads wishing to access obj1 will wait until some other
thread have acquired a lock over obj1. But what about obj2 ?
My conceptual doubt is ? In multithreaded scenario, synchronized bolck of objects of type, Object1, when applied, Synchronization applied to single object. So multiple locks are maintained for multiple objects. But within that Synchronized
block, if an object of type Object2 is modified within those different snippets, then, consistency with Object of type Object2 , is not maintained.
Now this is what I acquire from my knowledge of threading, I want to know, information I have is correct or wrong ?
If wrong whats wrong ?
If it is correct, then, to synchronize objects of type HashMap, will I have to put them in synchronized block, in order for them to stay consistent ?