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
posted
0
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.