The moose likes Threads and Synchronization and the fly likes Updating data Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Updating data" Watch "Updating data" New topic
Author

Updating data

Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
I want to write code to update an XML file. I need to use synchronization to prevent multiple simultaneoue updates or lookups while data is being changed. Is there an accepted pattern for writing code to do this? It seems like a simple idea until you start to actually write the code.
Paul
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Paul,
Well anytime you synchronize a method or object your goal is prevent two or more threads from modifying some certain data. The most straight forward way to do that is to synchronize whatever object that encapsulates that data. If your XML file is encapsulated as a Document inside any method that modified the Document you could call wait() on the Document object whenever another thread was already modifying. Of course once the modifying thread finishes, it must call notifyAll() on the Document object so any waiting threads can have their chance to play. I personally prefer to synchronize methods as opposed to objects since the latter is more prone to deadlocks even though it may take a few more microseconds to do so. I'm not familiar with any patterns associated with protecting data from concurrent modification but there may be. For the most part it's not as complicated as it seems so long as the data you're trying to protect is encapsulated in a single object.
Hope this helps,
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
 
 
subject: Updating data
 
Threads others viewed
display data when page is shown?
writing word file from java code
What is your style?
Small JDBC application.
write problem in text file
developer file tools