File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Synchronized Data Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Developer Certification (SCJD/OCMJD)
Reply Bookmark "Synchronized Data Methods" Watch "Synchronized Data Methods" New topic
Author

Synchronized Data Methods

Matt DeLacey
Ranch Hand

Joined: Oct 12, 2000
Posts: 318
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?
Thanks.
With Respect,
Matt
gautam shah
Ranch Hand

Joined: Oct 29, 2000
Posts: 72
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
2) CauseForsynchronized.java
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 file.in 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.
 
IntelliJ Java IDE
 
subject: Synchronized Data Methods
 
Threads others viewed
Lock design
Cleared the SCJD - 144/155
Should the criteriaFind be synchronized?
Dynamically specifying the size of an Array
XML Read/Write Concurrency Prevention.
MyEclipse, The Clear Choice