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 FileLock & Platform dependencies 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 "FileLock & Platform dependencies" Watch "FileLock & Platform dependencies" New topic
Author

FileLock & Platform dependencies

Jianping Wang
Ranch Hand

Joined: May 29, 2010
Posts: 60
Hi All,

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:

  • Must use only exclusive lock
  • Must use only one channel
  • Must not use memory mapping


  • 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?



    SCJP 6 with 93%
    Oracle Database SQL Expert with 98%
    Raf Szczypiorski
    Ranch Hand

    Joined: Aug 21, 2008
    Posts: 383
    Don't know about yours, but my assignment says something along the lines: no other application will use the same file, so you only need to synchronize it on a single application level (either synchronzied blocks or java.util.concurrent locks). If you use FileLock, you prevent other applications from accessing it, which is always nice, but not required by the specs.
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5218
        
      12

    Read your instructions carefully and I'm convinced you will encounter a paragraph similar to the one Raf has mentioned


    SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
    http://www.javaroe.be/
    Jianping Wang
    Ranch Hand

    Joined: May 29, 2010
    Posts: 60
    Thank you both, I really love this forum!

    Following is my assignment (URLyBird):


    Locking
    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.
    Raf Szczypiorski
    Ranch Hand

    Joined: Aug 21, 2008
    Posts: 383
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: FileLock & Platform dependencies