• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[URLyBird]Is my lock manager class right?

 
Zhixiong Pan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 284
Debian Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://faq.javaranch.com/view?ScjdFaq
After more than 70 posts you should know the posting policy.
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
1.you 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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Zhixiong Pan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic