• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Updating data

 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
What are you doing? You are supposed to be reading this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic