• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using if statements inside synchronized blocks

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 555
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sharma,
You should be safe unless your "doSomething" releases your synchronization (e.g. calls wait()).
Regards, Andrew
 
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic