Originally posted by ghanshyam sharma:
Hmm, good question.
Like lets say I am synchronizing on a hashtable X and inside the synchronized block I check the presence of a key/value pair in the hashtable using if statement, now since I have already acquired a lock on the hashtable using synchronized statement, even if I get sliced out inside this if statement, my code is 100 % right about my if statement condition. I mean I cant possibly have this particular statement changing the condition value on thread slice.
like
Synchronized (X) {
if ( X.containsValue("HAHA") ) {
//Do Something
}
}
what do you say ?
-sharma
Hi ghanshyam,
It depends on what 'do something' is. If it involves waiting, then no, you're not ok. That's because you're essentially giving up the lock @ that point by calling map.wait(). So the question is, does it involve waiting?
To be safe, I would probably do something like
All best,
M