Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Fundamental Locking Question

 
Dave Teare
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am only thinking about the locking mechanisims at the time, and had some very fundamental questions I would like help on before I start coding.
First, how does locking work - specifically, what do I synchronize on? I was planning on reading all the flights into a Hashtable keyed by the flight number, and sync'ing on the objects inside the Hash. i.e.
researveFlight(flightNumber)
flight = flightHash.get(flightNumber)
synchronized (flight) {
// Once we get in here, we know we
// can change this flight without interuption
}

When I read these forums you all talk about a LockManager class. My impl doesn't seem to need a lock manager, it simply sync's on the Hashtable elements. Am I missing something??

My second question relates to the first one. Is it a good idea to store all the flights in a HashTable; or should I lookup flight info from the Data class EVERY TIME a user does a search, lock, etc?
Thanks!!
--Dave.
[ October 29, 2002: Message edited by: Dave Teare ]
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Dave,
IMO, you don't need a lockmanager, unless it helps you think through the problem. In you case, it doesn't seem like it's neccessary. However, I'm not sure you need to lock on the items in your hashmap either. Depending on your implementation, you could just make the hashmap static, and simple insert elements into it as you need them(using either the client ticket or Thread as a key, and the recordno as the value).
The approach you're talking about has a nested lock structure(once for the map, then again for the element in the map), which, if I'm reading it correctly, is dangerous design, potentially opening the door to deadlock.
all best,
M, author
The Sun Certified Java Developer Exam with J2SE 1.4
 
Dave Teare
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your input! I'm glad to see I am not completely out in left field
I might still use a LockManager just because it seems to provide a clean seperation; I just wanted to make sure I used it for the right reasons.
--Dave.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic