aspose file tools *
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes NX: Synchronize read / find? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "NX: Synchronize read / find?" Watch "NX: Synchronize read / find?" New topic
Author

NX: Synchronize read / find?

Michael Couck
Ranch Hand

Joined: Nov 15, 2003
Posts: 46
Hi,
I have "read" and "find" methods in the interface that I was given, which I have implemented. I tested with multiple clients (30 000) doing finds first returning an int array of record numbers, which are used to read the data one record at a time and construct the model. However I find, which is to be expected, that there is a race condition on the threads and there is data corruption each thread not returning the correct number of records because another thread comes into the method and changes the vector before the original thread exits. By synchronizing the find and read methods there is no corruption. Has anyone else done this? Or is there a better solution? Or you don't care because 30000 clients connecting the server at the same time is unlikely? However there is always the possibility of corruption below 30000, at any level of active threads really. The probability just deminishes, never completely eradicated. :roll:
Regards
Michael
Bill Robertson
Ranch Hand

Joined: Mar 21, 2003
Posts: 234
If your method manipulates your file handle (which controls where your program is positioned within your file) in anyway you probably
want to synchronize on the file handle or the entire method.
(I say probably instead of must because I am not sure what you
design and intentions are - otherwise its a must).
Unless you find a way to perform multiple concurrent reads from
a single file that do not interfere with one another. Thats ugly
and beyone necessary.
Michael Couck
Ranch Hand

Joined: Nov 15, 2003
Posts: 46
Thanks Bill.
What I thought really, beyond necessary. The data is in a memory cache not the file, but no real difference there anyway. I checked the efficiency of syncronized read/find, against non-synchronized read/find and the difference was not large at all. So sync methods it is then.
Thanks again.
Michael
Dave Knipp
Ranch Hand

Joined: Oct 14, 2003
Posts: 146
Michael,
I dealt with this very same problem. I was worried about corruption of the database and i store mine in memory just like you store yours. Synchronizing on the read an find methods may not be enough though. I would assume you use a collection to store your records in memory correct? So synchronizing on the methods of Data will only lock that read/find method, it wont lock the underlying collection and therefore any other method such as create COULD change the data stored in your collection and therefore corrupt it. You may consider synchornizing on the actual collection every time you iterate on it, add, or delete from it. This will prevent from any concurrent modifications to the collection and will prevent your 'corruption' problem from occurring. What do you think?
Dave


SCJP 1.4, SCJD 1.4, SCWCD 1.3, SCBCD 1.3, IBM Certified Solution Developer -WebSphere Studio V5.0
Bill Robertson
Ranch Hand

Joined: Mar 21, 2003
Posts: 234
If you are using a cache, you absolutely want to
synchronize your collection. That has to be locked
writting to it and many people lock when reading from
it to prevent dirty reads, but thats your choice.
I did not do it this way but you can find a good
discussion on this here CACHE
Michael Couck
Ranch Hand

Joined: Nov 15, 2003
Posts: 46
Thanks guys,
True, true, I nearly made a really big mistake, never thought about that. That could have been 620 Euro in the bin.
Yeah, the read / find are sync, but the book / create are not. Sync the whole lot I say, can't hurt. I use vectors for the primary model, everything goes through this vector of vectors. Although Vector is syncronized, there is no guarantee that concurrent modification will not throw a concurrentmodificationexception at certain times, so the API says to sync programatically.
Thanks!
PS:Nearly finished! Whew!
See ya
Michael
Robert Huang
Greenhorn

Joined: Dec 15, 2003
Posts: 9
If you are using a cache, you absolutely want to
synchronize your collection. That has to be locked
writting to it and many people lock when reading from
it to prevent dirty reads, but thats your choice.

I agree !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NX: Synchronize read / find?