Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Synchronized Data Methods

Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The more and more I think about it, the more I don't understand
why most of the methods in Data are synchronized. I mean, for instance, what difference does it make if two threads are within criterFind or getRecord or find and so on...the only thing I can figure is that when add and delete and so forth are added, then having those methods be synchronized would be necessary. Is that the reason they are synchronized?
With Respect,
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
criteriaFind or getRecord, find or any other method which is dealing with that RandomAccessFile instance has to be synchronized with each other because in all these methods we are using seek atleast once . and there is only one RandomAccessFile instance so execution of any of the two methods which used seek in their body should not be executed simultaneously because every method needs different location in that file for reading.
wants an example ? download from here
this is a zip file . which contains two files
1) data.txt
place these two files in the same directory and then compile java code and run again and again . you will see the difference in some trys. if your work station is solaris then please enable the commented line "Thread.yield()" in the java the case of windows or unix you are not required to do any modification.
if you wants to see the correct output gautam shah
then you have to preceeded methods readFirstName and readLastName by keyword synchronized.
All the above explaination is according to my sense , and i am looking forward for any modification.
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic