• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using if statements inside synchronized blocks

 
ghanshyam sharma
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys.
Even though I am 100 % sure that my if conditions wont get effected due to thread slicing, still is it a bad programming practise to use if statements in synchronized blocks ???
If my dumb mind still remembers correctly Max's book didnt like it at all
-Sharma
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ghanshyam,
If you're 100% sure, then there's no practical reason not to. If you don'ty mind my asking, how are establishing this 100%?
All best,
M
 
ghanshyam sharma
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vlad Rabkin
Ranch Hand
Posts: 555
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ghanshyam,
I don't see any problem using if here. I do the same.
May I ask you: what for do you use hastable, but not HashMap, if you access it anyway withing synchronized block?
Best,
Vlad
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sharma,
You should be safe unless your "doSomething" releases your synchronization (e.g. calls wait()).
Regards, Andrew
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You should be safe unless your "doSomething" releases your synchronization (e.g. calls wait()).

Yes, and if "doSomething" releases your synchronization in some way, you could be safe anyway if you replace your if() by a while().
Best,
Phil.
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic