• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

lock/unLock which is the best pratice to be followed ?

 
Ramesh kumaar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
Incase of locking and unlocking which is the best Datastructure to be used among the followings:

ArrayList
HashSet
HashMap
I come to know some of our friends who have passed SCJD used clientID,recordNo for lock,unlocking, Some used only the recordNo alone for locking and unlocking. Here i like to have a discussion which is the best pratice to be followed?

Hope at the end of this disucussion we will be clear in this regard.

-rameshkumar
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are just using the record number use a HashSet, but you will wrap the record number into an Integer class.
If you are passing an Object that references a client, plus the record number, you could use HashMap. I think of the ArrayList as the replacement for Vector, which reminds me of Arrays, and HashMap is more a mapping of an object to an object, wish a HashCode, which makes searches faster?
Mark
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList is likely to be an inappropriate List in any case, since you'll have to remove elements (locks) from arbitrary places in the list -- an O(n) operation in ArrayList but an O(1) operation in LinkedList.
Know your collections! Do you care about the order of locks? No. Can you have duplicates? No: only one client can hold a given lock. Given this, you should immediately think: the locks form a Set. This can be either a HashSet or the keySet() of a HashMap. If you haven't internalised this type of reasoning during your SCJP preparation, then this is your opportunity. Go over the Collections again, buy the book, read the trail.
For completeness I should add that there is an alternative approach where you use an array that has one entry for each record in the database. Simple, but with a memory footprint that grows with database size.
- Peter
[ May 15, 2002: Message edited by: Peter den Haan ]
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter den Haan:
[...] remove elements (locks) from arbitrary places in the list -- an O(n) operation in ArrayList but an O(1) operation in LinkedList.
I should really add: ... if you're iterating through the list with an Iterator.
- Peter
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What should you use if you are reiterating?
Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic