Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Should synchronized methods call other synchronized methods?

 
Daniel Simpson
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys. My Data class is a Facade for both locking and database operations. The actual implementations in those manager classes (DataManager and LockManager) have synchronized methods. Is it necessary for my Facade to synchronize its calling methods, too? In my Data:

And then in my LockManager:

Basically, my question is whether or not it is necessary to synchronize methods in my Facade along with the actual implemented methods.

Thanks!!!
 
Murali Kurukunda
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just looking at the code you have posted I think the synchronization is not needed.

But it depends on how you have implemented your caching if at all you are using it. In my inplementation the cache is a static HashMap so inside the lock method I synchronize on the static map rather than making the instance method synchronized.

one more comment:
you are cahing the RecordNotFoundException and throwing it again in the facade method. I guess you might as well not catch it. Saves two lines of code [ January 14, 2005: Message edited by: Murali Kurukunda ]

[ January 14, 2005: Message edited by: Murali Kurukunda ]
[ January 14, 2005: Message edited by: Murali Kurukunda ]
 
peter wooster
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably don't need any synchronization in your Data class. I don't use any, and I avoid nested synchronization like the plague. If you do need nested synch, you must ensure that it's always done in the same order to avoid deadlock situations.
 
Daniel Simpson
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by peter wooster:
You probably don't need any synchronization in your Data class. I don't use any, and I avoid nested synchronization like the plague. If you do need nested synch, you must ensure that it's always done in the same order to avoid deadlock situations.

I have a static hashmap that holds cookies. I am synchronizing on my methods in the LockManager, so then it's not necessary to synchronize those methods in Data, too? To me, it didn't seem necessary to synchronize the calling and implemented lock method, but I just wasn't sure. Thanks! I'll remove all of the synchronized keywords from my Data facade.
 
Daniel Simpson
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Murali Kurukunda:

one more comment:
you are cahing the RecordNotFoundException and throwing it again in the facade method. I guess you might as well not catch it. Saves two lines of code


This sounds silly, but something just dawned on me. Is there even a point in catching an exception in either the facade or the actual implementation? Consider this code of my lock method:



Both checkThreadManager and checkRecordValid can possibly throw exceptions. But is it completely pointless catching and rethrowing in this lock method? Think about it. For instance, in local mode: I have a LocalAdapter that HAS-A Data object. I have a business method called book and it performs a book. But should I even catch the exceptions in there? The whole end-point of those exceptions are a to a JDialog. So can I just catch them in my GUIController and display the message instead of catching, rethrowing, catching, rethrowing all the way to the GUI? Or am I violating a software principle? Sheesh, does my question even make sense?
 
peter wooster
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Daniel Simpson:
So can I just catch them in my GUIController and display the message instead of catching, rethrowing, catching, rethrowing all the way to the GUI? Or am I violating a software principle? Sheesh, does my question even make sense?


I catch all exceptions in my Model and then use the Observer pattern to publish those as status events to any objects that are interested. My Controller subscribes to the event by calling model.addRoomEventListener, receives the event, uses invokeLater to get it onto the Event Dispatch Thread and finally displays an appropriate dialog. This allows my Model to have no references to my Controller or View or the Swing and AWT classes, its only interest is in the networking and data layers of my program and classes from java.util.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic