*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes multiple (non-nested) synchronized blocks in single method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "multiple (non-nested) synchronized blocks in single method" Watch "multiple (non-nested) synchronized blocks in single method" New topic
Author

multiple (non-nested) synchronized blocks in single method

Jared Chapman
Ranch Hand

Joined: Feb 23, 2004
Posts: 81
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?


B.S. University of Wisconsin<br />SCJP 1.4 (85%)<br />SCJD 1.4 (92%) B&S Contractors
Inuka Vincit
Ranch Hand

Joined: Aug 10, 2004
Posts: 175
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.


MCP (C# application dev 70-316) 860<br />SCJP 1.4 100% SCJD (URLyBird) 378<br />MAD 100% nuts
Jared Chapman
Ranch Hand

Joined: Feb 23, 2004
Posts: 81
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

Joined: Aug 10, 2004
Posts: 175
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: multiple (non-nested) synchronized blocks in single method