*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes How do my locking mechanism look? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "How do my locking mechanism look?" Watch "How do my locking mechanism look?" New topic
Author

How do my locking mechanism look?

Khaled Mahmoud
Ranch Hand

Joined: Jul 15, 2006
Posts: 361
//The code that initializes the locks table
Hashtable locks = new Hashtable();
The lock method should be synchronized so that no more than one thread can lock a record at the same time
This is the part which i could not do anything about it till this moment
Very tough
The lock hashtable will be composed of recordLock objects
inside the lock method

boolean lock succeed = false;
synchronized (locks) {
if locks.contain(recordLock) {
succeed = false;//The default case
recordLock = locks.get(recordLock);
} else { //The record has not been locked before,so lock the record
locks.add(recordLock,lockCookie);
succeed = true;
}
}
if (!(succeed)) {
recordLock.wait();
synchronized (recordLock) {
locks.add(recordLock,lockCookie);
return lockCookie;

}

else {
return lockCookie;
}

}


SCJP, SCJD,SCWCD,SCDJWS,SCEA 5 MCP-C#, MCP-ASP.NET - http://www.khaledinho.com/
Life is the biggest school
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

Hi, Khaled

A tip for start, try to use the UBB codes (http://www.javaranch.com)
This can make your code easy to read and to understand.

That was the good news.

Your code snippet will never works, some reasons can be :
1.if a thread enters and it pass the first if/else block and after this other thread locks a record your LM acts like the record is not locked.
2.wait is outside of a synchronize block - you try to make a thread to wait without to own the thread monitor, this is bad.
3.What you do with the IntteruptException ?

Regards M.
[ September 25, 2006: Message edited by: Mihai Radulescu ]

SCJP, SCJD, SCWCD, OCPJBCD
Khaled Mahmoud
Ranch Hand

Joined: Jul 15, 2006
Posts: 361
Well how about this:


[ September 29, 2006: Message edited by: Khaled Mahmoud ]

(quote -> code)
[ September 29, 2006: Message edited by: Barry Gaunt ]
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
You must understand well the concept of code synchronization, locks, and conditions in order to make your code work.

I think you should brush upon the concepts of multithreading in order to understand this better. It is actually simple once you understand it.

Here's some pseudo code.

To lock the record:



To unlock a record:



I hope that helps!
Khaled Mahmoud
Ranch Hand

Joined: Jul 15, 2006
Posts: 361
Can somebody please tell me what's wrong with this lock mechanism?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
What happens when you have compiled and run your code? Did it work as expected? Please do not expect people to correct your code for you. It is a requirement of SCJD that you can sort these problems out on your own.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
Consider Paul's rocket mass heater.
 
subject: How do my locking mechanism look?