I am still in design process for SCJD. After reading the official documentation of FileLock (http://java.sun.com/j2se/1.4.2/docs/api/java/nio/channels/FileLock.html) , I come into this conclusion that if I want my runme.jar works across platforms, then I have to follow all the rules listed:
So when query from the data file, the whole file is locked, and no other operation can be taken on it until the lock released. That's not performant at all. Beside, if my query fetches millions of records from data file, then the whole world is waiting for me . It seems pagination should be used here.
Am I right?
Following is my assignment (URLyBird):
Your server must be capable of handling multiple concurrent requests, and as part of this capability, must provide locking functionality as specified in the interface provided above. You may assume that at any moment, at most one program is accessing the database file; therefore your locking system only needs to be concerned with multiple concurrent clients of your server. Any attempt to lock a resource that is already locked should cause the current thread to give up the CPU, consuming no CPU cycles until the desired resource becomes available.
"At most one program is accessing the database file" means I can only lock the object that access the database file. Because if I use FileLock to lock a portion of data file, another thread can still lock another portion if they are not overlapped.
And the following from official documentation also discourages me from using FileLock.
File locks are held on behalf of the entire Java virtual machine. They are not suitable for controlling access to a file by multiple threads within the same virtual machine.
So I gonna adopt synchronized method.