My code for lock and unlock is as follows :
public class DataServer extends UnicastRemoteObject implements RemoteDataInterface
{
private ArrayList records = new ArrayList();
public void lock(int record) throws IOException, RemoteException {
try {
String element = ( String ) records.get(record);
if ( element.equals("LOCK") ) {
wait(); // causes the current
thread to wait until it is notified
} else {
synchronized ( records ) // lock the record object
{
records.add(record, "LOCK");
}
}
} catch(Exception e) {
System.out.println( e.getMessage() );
}
}
public void unlock(int record) throws RemoteException {
records.remove(record);
synchronized (records) {
// notifyAll unblocks the threads that called wait on this object
// this method can only be called within a synchronized method or block
notifyAll();
}
}
} // end of class
Is my approach correct for locking the current record for manipulation ?
Thanks
Ravindra