Forums Register Login

Difference between find() and criteriaFind()

+Pie Number of slices to send: Send
Hi,
Could any body discuss following point ?
find() method is Syncronised. then why signature of critriaFind is not Syncronised ? should we make it Syncronised or not required ?
please contribute your view.
thanks
devu.
+Pie Number of slices to send: Send
Well, that's an implementation detail, isn't it? It depends on how you implement criteriaFind().
- Peter
+Pie Number of slices to send: Send
Peter,
I am not clear with your clue 'its depends on....'
Because there is little in our hand. as clear instruction says 'it return datainfo, accepts string , it is to be in data class etc..' Now where is flefixibility in our hand ?? so how does it depends on how i imple.... ?'
My question was(is) basically fundamentally.
find method is just searching on dirst field and first mathching record..... and our criteriaFind will be more generic... BUT Both are fundamentaaly same. both have to read database by read() method.... dont u think that then even criteriaFind method also should be sysncronise ?
please correct me if i am somewhere wrong.
I know that most of people are clear about not to have criteriafind method sysnc. But i would appreciate if u could clear concept on why find is sycronise and criteriafind need not ?
sorry for lengthy question.
thanks
devu
+Pie Number of slices to send: Send
Methods need to be synchronized if they access Data's state or if they directly access the underlying file. So, is your criteriaFind() doing any of that, or does it do its job by calling other Data methods such as getRecord() that are already fully synchronized? This will determine if your criteriaFind() needs to be synchronized or not.
- Peter
PS. re: synchronized being an implementation detail: it is significant that the Java language does not allow the "synchronized" modifier with an abstract method.
[This message has been edited by Peter den Haan (edited September 25, 2001).]
+Pie Number of slices to send: Send
My take on it is this.
Strictly speaking, the synchronized modifier in Java is not a part of method's signature. (Remember your programmer's cert?). In other words, methods 'public int getIt()' and 'public synchronized int getIt()' have the same method signature.
I do think they ommited 'synchronized' deliberately just to confuse you.
findXXX should be synchronized. No doubt.
+Pie Number of slices to send: Send
 

Originally posted by Gennady Shapiro:
findXXX should be synchronized. No doubt.

Mmmm, I can write you a criteriaFind implementation that does not need to be synchronized (although it admittedly wouldn't be the most straightforward). Really, it is an implementation detail, and ideally a certified Java developer would be able to look at an implementation and determine if it needs synchronization or not.
- Peter
+Pie Number of slices to send: Send
Thanks peter and Gennady for reply.
BUT
still my question is at the same point. please note that I am not expecting directly code from u. I have already written criteriaFind method and is working fine.
BUT fact is that smallest find() method is uding seek(), readrecord() and my criteriaFind is also doing both things.
So if find() method is syncronised why not criteriaFind() ?
pl. guide .. at least tell me reason why find() is syncronised ?
(as methods that find() is calling are already syncronised!! then why we need find() to be syncronised ?
thanks
devu
+Pie Number of slices to send: Send
Why does find() need to be synchronized? Here is my take.
First off, let me state I struggled with synchronization in the Data class quite a bit at the start and I wasn't clear on what to synchronize and what not to. There is lots of discussion about it but people don't seem to agree. The thing that cleared it up for me was reading "Effective Java" (Joshua Bloch). In there he states you should *always* synchronize access to shared, mutable data. He gives a very clear explanation why this is so. I won't go into the details. So that is my rule: If I'm reading or writing shared, mutable data I synchronize it.
Let's apply this to the find() method. Notice that method uses the recordCount field in a for loop. What would happen if the recordCount was changed after your loop did the comparison to recordCount, but before it processed that record? It would be ugly--you might return data that had been deleted, or read data that hadn't been fully written.
In the book, he goes on to explain that overly clever people try to increase concurrency by not synchronizing atomic operations. This is dangerous and wrong.
Finally he states that developers that fail to synchronize properly often have the program work fine in testing. Picture the situation I described above in the find method. It is unlikely to happen--even if you do tons of testing. When it does happen, you might not blow up in the find() method, you'll probably crash elsewhere, which will make the problem nearly impossible to debug. Also, recreating the error will be almost impossible. A true debugging nightmare.
I'm sure I haven't explained this as well as he did, but hopefully that made sense.
The book again is "Effective Java" by Bloch. Great book.
+Pie Number of slices to send: Send
Hello,
My Q. still remain unanswered....
i request to clear my doubt.
As we use all methods of find() in our criteriaFind() method, why criteriaFind is not syncronised while find method is syncronised ??
(infact criteriaFind is more adv version of find method.)
+Pie Number of slices to send: Send
 

Originally posted by Devu Shah:
My Q. still remain unanswered....

There has been quite a bit of discussion about threads and synchronization so far. If you aren't able to decide at this point whether the criteriaFind() method needs to be synchronized or not, there are apparently still some gaps in your knowledge of the topic. Rather than hand you the answer on a golden plate, I'd strongly suggest you read up some more on threads.
- Peter
+Pie Number of slices to send: Send
 

Originally posted by mitchner green:
[Joshua Bloch] states you should *always* synchronize access to shared, mutable data. [...] he goes on to explain that overly clever people try to increase concurrency by not synchronizing atomic operations. This is dangerous and wrong.

Overly clever people like Sun? For instance, both Vector.size() and Vector.isEmpty() access the elementCount private variable but neither is synchronized in any way. Joshua's recommendation is a safe one, though; anything else is a performance optimisation that should be made only when strictly necessary, by an expert. Calling this "wrong" so categorically is, however, mistaken.
- Peter

[This message has been edited by Peter den Haan (edited October 01, 2001).]
If we don't do the shopping, we won't have anything for dinner. And I've invited this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 681 times.
Similar Threads
Passed with 145 points
Please help me for solving this
Passed 150/155
Passed with 150/155
Threads behaving weirdly
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 00:12:06.