This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes hi Mark! need your opinion.  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "hi Mark! need your opinion.  "lock()"" Watch "hi Mark! need your opinion.  "lock()"" New topic
Author

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

sanjeev mehra
Ranch Hand

Joined: Feb 12, 2001
Posts: 93
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 ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
sanjeev mehra
Ranch Hand

Joined: Feb 12, 2001
Posts: 93
----------------------------------
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

Joined: Feb 12, 2001
Posts: 93
hi Mark,
i am waiting for your response. pls. help me to clear my doubts.
regards,
sanjeev.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

(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
Laudney Ren
Ranch Hand

Joined: Jan 06, 2002
Posts: 111
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


" Veni, vidi, vici "<br />" I came, I saw, I conquered "
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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

Joined: Jan 06, 2002
Posts: 111
Yes.
I got it.
cpowell
Greenhorn

Joined: Mar 29, 2002
Posts: 2
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
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Yes, use the Integer wrapper class.
Mark
cpowell
Greenhorn

Joined: Mar 29, 2002
Posts: 2
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

Joined: Feb 12, 2001
Posts: 93
Hi Mark,
Can you give some detail of LockManager class. How it works?
Thanks and Regards,
Sanjeev.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: hi Mark! need your opinion. "lock()"
 
Similar Threads
Design patterns
How to synch in cluster?
several questions on B&S assignment
lock and unlock idea, need critics!
lock unlock