File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes The locking in the create() operation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "The locking in the create() operation" Watch "The locking in the create() operation" New topic
Author

The locking in the create() operation

Joe Zhou
Ranch Hand

Joined: Oct 18, 2006
Posts: 34
Hi gurus,

My question is: can we do create-locking the same way as update and delete?

Will the following algorithm work:

creat(String[] data):
1. Search the file for a deleted record or a record at the
end of the file.
2. Lock the record.
3. Write the data.
4. Unlock the record.

My studies show that the above algorithm runs into race-condition.
Not sure if I am right. I don't hear any talking on this topic.
Any comment would be appreciated.

Thanks,

Joe
Mike Ngo
Ranch Hand

Joined: Oct 16, 2006
Posts: 89
what is your logic for locking during create()?
Joe Zhou
Ranch Hand

Joined: Oct 18, 2006
Posts: 34
The same one as for update and delete as many gurus talked.

synchronized(lockObj){
while(isLocked(recNo)){
lockObj.wait();
}
lock the record;
}
Mike Ngo
Ranch Hand

Joined: Oct 16, 2006
Posts: 89
when you update/delete records those are existing records. Since many clients may try to access the same one at the same time, they need to be locked. Create() insert new record so there is no locking involved.
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
Correct. Locking is on record level and during a create there's no record (yet) so there's nothing to lock.


42
Joe Zhou
Ranch Hand

Joined: Oct 18, 2006
Posts: 34
How about two clients try to 'create' and both find the same deleted record
to place the data?
Sam Codean
Ranch Hand

Joined: Feb 26, 2006
Posts: 194
i synchronzize the CRUD methods so that at a time only one operation is performed. You cannot allow Read and write simultaneously to the file so this solves the issue of two creates finding the same record number to be created


-Sam Codean<br />SCJP 1.4 (98%)<br />SCJD 5.0 (87.5%)
Joe Zhou
Ranch Hand

Joined: Oct 18, 2006
Posts: 34
Thanks for the posts. Better understanding...
The synchronized mehtod may not block threads from calling on the
different objects.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The locking in the create() operation