• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NX: Please look my work for locking

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the document of sun give me ,
<Your server must be capable of handling multiple concurrent requests, and as part of this capability, must provide locking functionality as specified in the interface provided above. You may assume that at any moment, at most one program is accessing the database file; therefore your locking system only needs to be concerned with multiple concurrent clients of your server. Any attempt to lock a resource that is already locked should cause the current thread to give up the CPU, consuming no CPU cycles until the desired resource becomes available >
I want to do like this:
1) I will have a LockManager.java whitch has a HashMap , The key is recordNo , the value is lockCookie. There are some methods like:


2) in the bookng of client:

Does these ok ?
Please Help me . thank you very much!!!

[Andrew: put the source code between [code] and [/code] UBB tags]
[ August 02, 2004: Message edited by: Andrew Monkhouse ]
 
Ranch Hand
Posts: 697
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John
Welcome to Javaranch and SCJD forum

Originally posted by CN John:



I am a bit confused here. Once you lock the record you are making available true. In the while test in lockRecNo you test if available is false. I am not sure if it works as locking because if one thread locks the record and make available true, the next thread checks fails while condition and again locks the record which is not what we want. The test in while in lockRecNo should be complementary to what is now.

I did'nt understand rest of code and so cannot comment on it. Maybe someother can help. You can check locking code in Max Habibi's book which gives a perfect idea of how locking works. You can build on it or can stick with it.

Good Luck.

 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi CN,

Welcome to JavaRanch and this forum.

I have edited your post to put the code between [code] and [/code] UBB tags. Doing this ensures that indenting is preserved, which makes the code easier to read.

When you are writing your post, there are a number of buttons just below the edit pane, which will insert the tags for you, so you don't have to remember what they are.

If you would like to edit your original post so that you can see what I have done, you can click on the button that is just above your post.

I have also removed some of your posted code, as we do not allow major sections of the assignment to be posted in this forum. The locking code is worth 20% of the assignment, and this is far too much to post in total.

There are multiple reasons for this policy:
  • Sun do not allow you to share your assignment or a solution to the assignment with others.
  • You have spent time and effort getting your solution right. It would not be right for someone else to just copy your solution without working it out for themselves.
  • If someone did get awarded the SCJD certification after copying your code, and were then given employment because they had that certification, the employer would probably find that the employee cannot actually do the work. Which makes the perceived value of this certificate decrease.
  • If people post too much code, then Sun may, in the future, request that we do not allow any SCJD code to be posted.

  • This policy is described in the question "What is the policy on posting questions I saw on the exam / details of how to do the assignment?" in the JavaRanch SCJD FAQ.

    Regards, Andrew
     
    Andrew Monkhouse
    author and jackaroo
    Posts: 12200
    280
    Mac IntelliJ IDE Firefox Browser Oracle C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi CN,

    Satish has already commented on the confusing issue of you setting "available" to true. It actually appears that this "available" flag is working as a Mutual Exclusion (mutex) flag to ensure that only one thread can be doing the lock work at any given time. But you already have your code inside a synchronized block, which provides the mutex for you - only one thread can ever be running within that block.

    If you remove that "available" mutex, then you will probably find that you no longer need the call to notifyAll().

    You should never swallow an exception in the way you are doing - you should always have some sort of handler for it or allow it to be thrown to the calling class so that it can handle the exception.

    In the booking of the client you will probably want an extra step - checking that the record is still available.

    Regards, Andrew
     
    Ranch Hand
    Posts: 531
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This code may suit your needs better:



    In this code your thread acquires a lock on map, then checks if the record number it wants to lock is already locked. If not, it locks the record and tells other waiting threads the map is unlocked for them to do their own checking. If yes, it does the following thing repeatedly until the record is no longer locked:

    1) locks on the map object.
    2) checks if record is locked in the map object.
    3) releases the lock on the map object, allowing other threads to do some work.

    Glad if you find this helpful.
    [ August 03, 2004: Message edited by: Anton Golovin ]
     
    Satish Avadhanam
    Ranch Hand
    Posts: 697
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Anton
    I see that you are calling notifyAll() after doing a lock. You may want to think about using notifyAll() in lock method and how useful it is.

    Satish.
     
    Ranch Hand
    Posts: 823
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi all,

    Do we know if locking at the database level (e.g. a whole Map), as opposed to the record level, is sufficient (if implemented correctly) to score full marks for the locking component? It strikes me as a bit of a weak solution, effectively preventing concurrent writes to the DB. Of course, if it wins full credit, it's a simpler solution than record locking...

    Jules
     
    Anton Golovin
    Ranch Hand
    Posts: 531
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Julian Kennedy:
    Hi all,

    Do we know if locking at the database level (e.g. a whole Map), as opposed to the record level, is sufficient (if implemented correctly) to score full marks for the locking component? It strikes me as a bit of a weak solution, effectively preventing concurrent writes to the DB. Of course, if it wins full credit, it's a simpler solution than record locking...

    Jules



    We're locking record numbers. When you write or read from the database, you write to a region. Locking a record in the Map ensures no other thread will be writing to the part of the file. They may write to other parts of the file.
     
    Anton Golovin
    Ranch Hand
    Posts: 531
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Satish Avadhanam:
    Hi Anton
    I see that you are calling notifyAll() after doing a lock. You may want to think about using notifyAll() in lock method and how useful it is.

    Satish.



    I think it's pretty useful. But I'd like some advice, if you have a minute to share your opinion. Always interested in other, more experienced developers' opinion.
    [ August 03, 2004: Message edited by: Anton Golovin ]
     
    JulianInactive KennedyInactive
    Ranch Hand
    Posts: 823
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Anton,

    I can see I've got the wrong end of the stick here. I really should refrain from posting late at night. I was thinking that you were actually synchronizing on a representation of the database file (the map) and performing a DB write in the critical section, but after a second (proper) look it's clear that the code is effectively part of a lock manager that takes locks at the record level.

    Thanks for your clarification anyway.

    Jules
     
    Satish Avadhanam
    Ranch Hand
    Posts: 697
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Anton Golovin:


    I think it's pretty useful. But I'd like some advice, if you have a minute to share your opinion. Always interested in other, more experienced developers' opinion.

    [ August 03, 2004: Message edited by: Anton Golovin ]



    Anton, please refer here.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic