• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple Lock question

 
arun s kumar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi:
I have a simple question, just started working on the assignment, can someone please
explain this:
I see in most of messages posted on locking the code is something like this :
1) public void lock(int recNo, Object client) {
synchronized(hashMap) {
....
}
...
}

2)
public synchronized void lock(int recNo,Object c)
{
...
}
Now if there are 100 clients and 100 records and
each client is requesting to lock one of these
100 records, only one of them will get into the
synchronized block, remaining 99 will have to wait till the one in the synchonized block completes it operation or goes into wait state. Ideally all 100 should have been able to acquire the lock simultaneouly because they are requesting for different record numbers.
Now suppose if i code something like this :
Object a[] = new Object[100];
for (i=0; i<100;i++) {
a[i]= new Object();
}
HashMap hMap = new HashMap();
public void lock(int recNo, Object client)
{
Object obj = a[recNo-1];
Synchronized(obj) {
... // usual hashMap code
}
}
}
In the above i have one object to synchronize
for one record, so if there are 100 clients
requesting to lock 100 different records they
can lock simultaneously.
If the reasoning is not right can someone please explain.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arun,

Now if there are 100 clients and 100 records and
each client is requesting to lock one of these
100 records, only one of them will get into the
synchronized block, remaining 99 will have to wait till the one in the synchonized block completes it operation or goes into wait state. Ideally all 100 should have been able to acquire the lock simultaneouly because they are requesting for different record numbers.

How many nanoseconds are we talking here? 1000?, 2000? Each client is going to either lock or wait in a "New York" second.

In the above i have one object to synchronize
for one record, so if there are 100 clients
requesting to lock 100 different records they
can lock simultaneously.

Problem is, how do you know how may clients you have or records for that matter (records could be added dynamically to the database). If you take an approach like this you'll need dynamic storage for your objects and not a static array. Let's also consider probability. It's doubtful in an application such as this that there would ever be more than twenty or so clients. How often will all twenty request locks at exactly the same microsecond? For that matter, how often will two? Code like you propose is hard to follow and unless there are benefits beyond what I can see, I don't think it's worthwhile.
That's my dos centavos,
Michael Morris
 
arun s kumar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Morris, i just wanted to hear from a
expert like you. In fact i did not implement this
way myself.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic