OK, so how many times has this question been asked.
But I still can't find a good answer that I like. So I am posting my lock code in the Data class, and the RemoteDataAccess class. I have tested two concurrent clients, and had one lock record #3 and loop for a few seconds, then I had the second client try to lock the same record, it waited until the first class unlocked the record before it could get a lock.
The only problem, is I have a while loop, instead of a wait()/notify(). Is this wrong?
*********lock method in my data class
public void lock(int record) throws DatabaseException{
while(lockedRecords.indexOf(new Integer(record)) < NOT_IN_LOCKED_RECORDS){}
lockedRecords.add(new Integer(record));
System.out.println(lockedRecords.toString());
}
}
******lock code in my RemoteDataAccess class
public void lock(int record) throws RemoteException, DatabaseException{
int recordIndex;
if (record == LOCK_ALL_RECORDS){
for(int loopCount = 0; loopCount < getRecordCount(); loopCount++){
lock(loopCount);
}
recordIndex = lockedRecordIndexInVector(record);
} else {
recordIndex = lockedRecordIndexInVector(record);
if (recordIndex == NOT_IN_LOCKED_RECORD){
FBNDatabase.lock(record);
lockedRecords.add(new Integer(record));
}
}
}
lockedRecords is a Vector.
Is my code threadsafe, or a big risk?
Thanks
Mark