File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Lock and Search Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Lock and Search" Watch "Lock and Search" New topic

Lock and Search

Radi Hadzhiyski
Ranch Hand

Joined: Jun 30, 2009
Posts: 47
Hello there,

this is my first topic in this forum and first i want to apologize my english

So i have two questions:

I. (ReentrantLock resp. ReentrantReadWriteLock + Condition)
or (synchronized + wait/notify/notifyAll)

II. Search

I. I read the chapters of the SCJD book that handles Lock issues
and want to know if i have understood it:

I can make my methods to be synchronized and so i can use wait and notify,
if i see that some other thread uses my record already, so i cannot lock it just like:

or i can use the Lock interface:

and if i want to have separate access (read or write) i can use new ReentrantReadWriteLock()
instead of new ReentrantLock() and i must then write lock.readLock().lock resp. lock.writeLock().lock
after that lock.readLock().unlock resp. lock.writeLock().unlock.

1. Am I right, how i explain it?

2. if synchronized+wait/notify is the same like Lock+Condition,
how should i choose between the two of them for my assignment

3. if i write lock.signal()/notify() resp. lock.signalAll()/notifyAll(),
is what preferable, single or all?
(if i choose single only one thread is notified,
so when it is done, it notifies another thread an so on,
but if i choose all, then all threads are beating and only one wins,
so it is only a matter of performance, isnt it?)

II. It must allow the user to search the data for all records,
or for records where the name and/or location fields exactly match values specified by the user:

Here i want to ask, if i have understood it in the right way:

1st possibility: i make my gui so, that the user has three radio buttons to choose between search for all,
search with OR, search with AND (and two JTextField for name and location)

2nd possibility: i make my gui so, that the user doesnt have any radio buttons,
but only two JTextField for name and location and if he dont type anything in the fields he becomes "search fo all",
if he writes only in one field, he becomes "search OR"
and if he writes in both of the fields then he becomes ""search AND"

So what is right?


SCJP 1.6 (91%), SCJD
Roel De Nijs

Joined: Jul 19, 2004
Posts: 8364

Hi Radi,

Welcome to the SCJD-forum

For your 1st question: i used the wait/notify/notifyAll (with making my methods synchronized). I prefered this approach because it is so easy and simple. I don't have experience with the new locking api, so can't help you with that. But one significant remark: using just notify won't do the job, you have to use notifyAll, because if you use notify you are not sure which thread is woken up to start running. It could be a thread which is waiting on another record than the one that was unlocked, so it will check the condition, go back into wait state and the thread that was waiting for the unlocked record will still be waiting and waiting and waiting (until another thread has unlocked a record and hopefully then that thread is woken up, but again no certainty).

For your second question: Here is no good or no wrong. Just provide the search requirement and you will be fine. This assignment is all about making decisions and argumenting why you implemented it like that and not used an alternative. So I implemented your 2nd possibility, because it's again very easy, quick and simple for the user (just fill the search criteria you want to search on and hit search-button).

Kind regards,

SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
Radi Hadzhiyski
Ranch Hand

Joined: Jun 30, 2009
Posts: 47

thanks Roel De Nijs,

yes, you have right with the notify vs. notifyAll, but wish some more opinions about the searching thing (because it is a MUST thing), so i dont want to failure only because of this.

and one other thing about synchronized vs. Lock:

if i use public synchronized find() and public synchronized delete() and so on, it is a poor design i think, because:

if one client searches for something, no other client can do anything, so it has to wait, but if you have read/write locks, or if you use synchronized(something) {} // block and not the whole method it is a better performance:

what do you think? can i use:

Roel De Nijs

Joined: Jul 19, 2004
Posts: 8364

Hi Radi,

Regarding the searching thing: if you implement your 2nd possibility, you will certainly not fail (because I did it too and I passed). But again: if you make sure the user can search on these 4 different ways (all records, location only, hotelname only, location and hotelname) you will be just fine. How you represent it in your gui will have no effect on you failing: it will cost you some points in the gui section if it's too complicated, not user-friendly,... But if user can search these 4 different ways you don't risk failure.

I declared all methods synchronized and too my opinion that's not a poor design, the main drawback is: the application looses a bit performancy. The main advantage is that it is very easy and simple, and also very understandable for (and easy to explain to) a (novice) junior programmer. And because my instructions told me a clear, simple and easy design/code is preferred to a complicated (but performanter) I chose the "poor design" approach.

Kind regards,
Radi Hadzhiyski
Ranch Hand

Joined: Jun 30, 2009
Posts: 47
Ok but there is something else, here my interface method from sun:

so if the record has the fields:
name, location, size, smoking, rate, date, owner

the user can only type name and/or locaton (two JTextField), so my criteria array looks like this:
So, null should match everything, so:

1. if i type only in the field for name: i have {name,null,null,null,null,null,null}
2. if i type only in the field for location: i have {null,location,null,null,null,null,null}
3. if i type in both fields: i have {name,location,null,null,null,null,null}
4. no type in fields: i have {null,null,null,null,null,null,null}


1. is an example for OR search for name
2. is an example for OR search for location
3. is an example for AND search for name and location
4. is an example for search for all records


there is also another example for OR search that cannot be satisfied by this:
5. type in both fields and i want to have all the records that has this name (no matter what location) and that has this location (no matter what name)

For example:

id name location ...
1. aa loc1
2. bb loc1
3. cc loc2
4. cc loc2
5. cc loc3
6. dd loc4
7. ee loc5

and assume that i want to type in my gui in both fields for name: cc and for location: loc1
so if i assume that my program does AND search if i typed in both fields i receive no records. But it can be so, that i want to receive the records that has
either name:cc or location:loc1. Then i should receive record: 1, 2, 3, 4, 5

So my find method should know what to do, if a typed in both fields something. Should he search for AND or for OR

What do you mean: is that necessary to define also the 5th kind of search, or the the first 4 are enough?

Because the sentence:
It must allow the user to search the data for all records, or for records where the name and/or location fields exactly match values specified by the user.

includes that:
It must allow the user to search the data for records where the name or location fields exactly match values specified by the user.

So what it means OR is not unambiguous. It can be only that i should provide the possibility to search with only one field typed in (1st and 2nd)
or the other thing (5th)

Roel De Nijs

Joined: Jul 19, 2004
Posts: 8364

Hi Radi,

There is indeed a difference between the search in the gui and the results returned from the find-method in the Data class. That's why you have to do some filtering (because find in the data is "starts with", at the gui side it's expected to be "exact" matches). My search in the gui just does 4 searches (like I already stated in a prvious post) and that seemed to be enough. If you want to add your 5th possibility that's also ok, but then you will have to create a gui that will be a bit more complicated (radio boxes, combo box with "and" + "or" to combine search criteria,...)

About this topic there are already a lot of other discussions/threads in this forum and i don't like to repeat myself, so i'm just gonna point you to these threads and I think you will find a lot of valuable information and answers to all your questions:
  • thread 1
  • thread 2
  • thread 3

  • Kind regards,
    Radi Hadzhiyski
    Ranch Hand

    Joined: Jun 30, 2009
    Posts: 47
    I have read all the threads and saw what the opinions are. But
    i want to tell you my assumption and you tell me what you think about it:

    null mathes everything
    non-null mathes everything that starts with criteria[n]

    these two sentences above say only how the specific field is matched or not. They
    say not how the specific record is matched. The sentence:
    "Returns an array of record numbers that match the specified criteria."
    say how records are matched and since criteria is a plural, a record is only matched if
    all criteria[n] from 0..n are null or if there are a non-null criteria[n], then the record is matched if the field
    of the record starts with the non-null criteria[n]. So ANDed and not ORed

    for example:

    in db following 3 records:
    a) Ritz, Antwerp,...
    b) Ritz, Paris,...
    c) Hilton, Antwerp,...
    d) xxx, yyy
    e) mmm, nnn

    I) user enters {Ritz,Antwerp,null,null,null,null,null}
    you mean the find should return a,b,c records because of ORed, but if it is so, then by
    II) user enters {Ritz,null,null,null,null,null,null} it should returns all records because the location
    is also ORed, but this time it is null, witch matches everything.

    and the other thing for the future:

    in the gui:
    if a search for name only i invoke find ({name,null,null,null...}) // no filter in the gui
    if a search for location only i invoke find ({null,location,null,null...}) // no filter in the gui
    if a search for name AND location (both are entered) i invoke find ({name,location,null,null...}) // no filter in the gui
    if a search for name OR location (both are entered) i invoke the find method two times:

    recordsName = find(name,null, null,...)
    recordsLocation = find (null, location, null, null, ....)
    than merge recordsName and recordsLocation

    thats all

    Am I right ?

    What do you think? What do the another people in this forum think?
    Roel De Nijs

    Joined: Jul 19, 2004
    Posts: 8364

    Hi Radi,

    Again one of the decisions you have to make and if that's your interpretation than that's good enough. If you document it in your choices.txt you'll be fine.
    My find method in Data class works as follows:
  • with an array with all null-values all records match (and are returned)
  • otherwise a record matches (and is returned) if it starts with at least 1 non-null value (so the null-values are ignored)

  • That was my opinion about how the find method should work and that's how I implemented it. If you use the search-function of this forum you will find 1000 of threads about find-method and should they be AND'ed or OR'ed. So it's 1 of the hot topics about this assignment. And i think the majority is OR'ing them, only a few are AND'ing them (but this choice won't affect your grade)

    Nonetheless if you will use AND or OR, you have to apply filtering in the GUI, because the gui expects "exact" matches and no "starts with" matches. Unless of course you don't use text fields and use combo boxes with all the occuring names and locations in your database file. An advantage would be user can't make typos, a thinking point would be: when and how will you refresh these combo boxes?

    Kind regards,
    I agree. Here's the link:
    subject: Lock and Search
    It's not a secret anymore!