• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do my locking mechanism look?

 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}

}
 
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebody please tell me what's wrong with this lock mechanism?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic