File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do my locking mechanism look?

 
Khaled Mahmoud
Ranch Hand
Posts: 361
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//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;
}

}
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Khaled Mahmoud
Ranch Hand
Posts: 361
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 961
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 361
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can somebody please tell me what's wrong with this lock mechanism?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic