aspose file tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes [URLyBird]Is my lock manager class right? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "[URLyBird]Is my lock manager class right?" Watch "[URLyBird]Is my lock manager class right?" New topic

[URLyBird]Is my lock manager class right?

Zhixiong Pan
Ranch Hand

Joined: Jan 25, 2006
Posts: 239
Hi all,

Thanks for caring my problem. I have finished my LockManager, can you give me some advices on it. In Data' lock, I call lock in LockManager. And in Data's unlock, I call unlock in LockManager. Before showing the code, see following instructions:
1. I add a HashMap type var lockThreadMap to LockManager. If a record has never been locked, then the first locker thread will be put into lockThreadMap with the recNo as key.
2. In lock, if a record not yet locked, then nothing special. If a record already locked, then get the thread from lockThreadMap with the key recNo and wait on that thread.
3. In unlock, after lockMap remove the lockCookie via key recNo, I fetch recNo corresponded thread from lockThreadMap and call that thread's notify.

Appendix: the LockManager

<Big code portion removed - see below>

I am hoping your replies.
[ April 18, 2006: Message edited by: Barry Gaunt ]

Oricio Ocle
Ranch Hand

Joined: Nov 30, 2004
Posts: 284
After more than 70 posts you should know the posting policy.

Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 918

Hallo Pan

use standard java code formating - the code is more easy to read/uderstand.

0.lockCookie = System.currentTimeMillis() -> not so nice I have a computer with a broken bios - the date is always the same. use nested synchronized blocks - this can lead you to a dangerous dead lock swamp. Maybe the currnet code works fine but if someone tries to imporve(change it can generate very easy a dead lock -> this methodology is not flexible enought.
2.By any exception your LM generate a cookie a valid one.
3.It seams that yout LM locks all the threads thats enter in the lock method - a nice feature - but I am not shore that you need that.
4.Consider scenario : a client I lock a record and I do Thread.currentThread().nitifyAll().
5.To many Integer instaces - think about(inboxing) it.

There are also others but now I must deep down in my own project(s).

Regards Mihai.

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Please do not post complete portions of your code. See this part of the SCJD forum FAQ.

For questions / discussions on the assignment, in general we will allow discussion where members are trying to:

* understand the assignment

* get a suggestion on how to work on a particular problem,

* are asking for feedback on small sections of code

* get suggestions about which tools to use.

But we will not allow posting major parts of a solution. For example, you may not post your entire code for locking and unlocking records - this is a major section (currently worth 20% of the assignment) and sections of such code will be removed from any post. Posting just one method (e.g. the lock() method is usually considered reasonable).

(Original in TC)
[ April 18, 2006: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Zhixiong Pan
Ranch Hand

Joined: Jan 25, 2006
Posts: 239
Oh, I am so sorry for my breaking the rule in this forum. I will be a legal rancher here after this mistake.

Regarding the lockManager problem, I am clear now.One simple way is let all clients thread waiting on lockMap. And in unlock after the remove the lockMap, it also call notify.
It is sorta covered in the JavaRanch Style Guide.
subject: [URLyBird]Is my lock manager class right?