Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between find() and criteriaFind()

 
Devu Shah
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that's an implementation detail, isn't it? It depends on how you implement criteriaFind().
- Peter
 
Devu Shah
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Gennady Shapiro
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Devu Shah
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
mitchner green
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Devu Shah
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic