• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

hi Mark! need your opinion. "lock()"

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,
Pls. explain locking process.
1. It is for record level locking. But when a process is syn. (syn. {read, modify, write}). Then how can multiple users perform same process for diff. records simultaneously?
2. How can I get lock on a particular record? A record is not an object.I have read, store the record no. and if someone request the same no. which is in list, asked the thread to wait().Is this the way to perform lock()?
But 1st point is not clear to me, pls. give me your time.
Thanks.
Regards,
Sanjeev.
[ March 25, 2002: Message edited by: sanjeev mehra ]
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't think of this locking schema as a record lock as you know in Oracle or DB2, or other DBMS. This is a self imposed "Lock", which is just that the record number is in a HashSet, and you should wait till that user removes it from the HashSet when it calls unlock()
As to your first point, lock is synchronized, when a client calls lock it either locks the record but putting the record number into the HashSet. If another client has the lock, (That record number already exists in the HashSet), then you call wait(), by calling wait, you allow another thread to call lock() which might be requesting a lock on a different record, that is not in the HashSet, therefore that second client gets the lock for a different record.
Mark
 
sanjeev mehra
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
----------------------------------
As to your first point, lock is synchronized, when a client calls lock it either locks the record but putting the record number into the HashSet. If another client has the lock, (That record number already exists in the HashSet), then you call wait(), by calling wait,

you allow another thread to call lock() which might be requesting a lock on a different record, that is not in the HashSet, therefore that second client gets the lock for a different record.
Mark.
---------------------------------
Hi Mark,
Thanks for you response. What I have learned is that
lock() is nothing but (syn. {check the record_no in the HashSet + if not found, put record_no in Hashset + read record + modify record + write record}). If request by another thread for the same record comes, that thread will execute wait(). (is this for to save processor resources?).
Hear is my problem,
If request from another thread for diff. record comes, I think, same object can't execute request simultaneously. Because lock() process is syn. and it can't serve multiple thread. Even for diff. record no. But this is not record level locking, so I am not correct.
Mark you patience can help me to pass the exam. Pls. correct me where I am wrong.
Thanks.
Regards,
Sanjeev
 
sanjeev mehra
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Mark,
i am waiting for your response. pls. help me to clear my doubts.
regards,
sanjeev.
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

(is this for to save processor resources?).


No, it is to allow other clients to call lock. When a thread has a lock on an object, it is exclusive, no other client can call or use it, until the thread is done with it, or until it is put into a wait state. That is why wait() notify(), and notifyAll() are all defined in the Object class. This way every single object ever created in Java will have those methods.
And in all honesty I only synchronize on the HashSet. as in here is the beginning of my lock method

lockedRecords is my HashSet
Hope that clarifies things for you.
Mark
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question here:
Since "record level lock" is required, it's ideal for different threads to access different records simultaneously at any time point.
However, since when a thread tries to lock (or maybe also unlock), it synchronizes on the HashSet. During this period, other threads accessing other records are also blocked out!!
So, maybe an ideal solution is to make "lock objects", which in Mark's case is the HashSet, completely isolated (or divided) for each record. For example: if you have 3 records in total, create 3 objects, each representing one record. Thereafter, lock on a record equals a lock on the corresponding object. Since 3 objects are completely isolated, ideal record-level access is realized.
-- Laudney
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Laudney - When a client has a lock on the HashSet it does not stop another client from working on another record at all. All the HashSet has is record numbers that are currently locked, not the records themselves.
And besides that the lock on the HashSet at anytime will be in milliseconds.
Mark
 
Laudney Ren
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.
I got it.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,
I was reading the previous post and I have a question. How do you add the record 'number' to a HashSet? Do you use a wrapper class? To my knowledge you can't add a primitive to a HashSet using the HashSet.add() method.
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, use the Integer wrapper class.
Mark
 
cpowell
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So why not just add the record (DataInfo) class and call the getRecordNumber() method on it? Are there any potential drawbacks to this approach?
 
sanjeev mehra
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,
Can you give some detail of LockManager class. How it works?
Thanks and Regards,
Sanjeev.
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I didn't use a LockManager(although I should have), so I wouldn't be the best to ask. Try doing a search on this forum on LockManager, I am sure you will find some great posts on it.
Mark
 
A timing clock, fuse wire, high explosives and a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic