Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Synchronization Q

Status
Not open for further replies.

DKL01

Programmer
Sep 14, 2000
233
US
Hi, I'm writing a RMI based client server application. The client allows to
read and update records. I'm writing lock and unlock methods that so that
only one client can update a particular record at a time.

At server I'm having vector to hold locked records. My original plan was to
synchronize LOCK and UNLOCK methods. But I thought this is not a good
design. Consider the following situation :

1. Client A wants to lock record 1
2. Client B wants to lock record 2

Why should Client B wait until Client A completes Locking process
though Client B wants to lock different record ?

So I'm thinking of having lock on each object in the vector instead of
synchronizing LOCK & UNLOCK methods. But confused about logic.

I REALLY APPRECIATE SOME IDEAS / SUGGESTIONS..

Thanks
 
I think that the second option is the good one.
The code should be something like this:
public void lock(int recordNumber)
{
synchronized(theRecordArray[recordNumber])
{
do it...
}
}
this only works if theRecordArray contains Object

Hope it helps!!!!
 
IMHO locking should always be minimal. You should only lock the necessary objects. However you may want to take a look at the Poolman Package. It is a very powerful object/connection pooling approach ( ) - not really what you wanna do here but closely related.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top