• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

multiple (non-nested) synchronized blocks in single method

 
Jared Chapman
Ranch Hand
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I just decided to switch my implementation of Data.java to implement caching. I am caching the entire database, and writing to file from the methods that modify data (delete, update, create). My question is on using multiple synchronized blocks in a single method, and whether this is bad style or not. Consider the following:

I have 2 synchronized maps, one for keeping track of locked records, and one for storing actual records. Let's call them lockedRecords and recordCache. Also, each client shares one single instance of Data. Now consider my implementation of update:

Is this implementation acceptable?
 
Inuka Vincit
Ranch Hand
Posts: 175
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
two quesitons. Assuming we are looking at Data.
Is it neccary to check the lock on the update? Not in the requirments but most important of all cant do this if your implementing DBMain right because the DBMain update doesnt throw a security excpetion? (ofcourse your assignment requirements might be diffrent from mine).

Secondly when you update and read the cache if you use synchronized container type then it wont be neccary to synchronize. Thats how I am doing it anyways.

So far so good.
 
Jared Chapman
Ranch Hand
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Inuka,

Thanks for your reply!

Is it neccary to check the lock on the update?

I have B&S Contractors v 2.1.2. My update method signature is:

so I do need to check the lock. I lock recNo in my DataAdapter, but inside update I make sure that lockCookie matches the cookie recNo was locked with.

when you update and read the cache if you use synchronized container type then it wont be neccary to synchronize.

Forgive my ignorance, but I fall under the category of "entry-level"
So by creating a Map as follows:

you are essentially creating a map that functions like a HashMap whose methods are all synchronized, as opposed to:

where you are creating an actual HashMap whose methods are not synchronized?
[ November 02, 2004: Message edited by: Jared Chapman ]
 
Inuka Vincit
Ranch Hand
Posts: 175
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly your in the right path. Thhere are certain operations that may cause problems but using locks should deal with them.

possible problem cases are covered in this article
http://www-106.ibm.com/developerworks/java/library/j-jtp07233.html

apart from that issue then your right, didnt know the requirements for your update.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic