This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FileLock & Platform dependencies

 
Jianping Wang
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
     
    Raf Szczypiorski
    Ranch Hand
    Posts: 383
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Sheriff
    Posts: 9934
    113
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Read your instructions carefully and I'm convinced you will encounter a paragraph similar to the one Raf has mentioned
     
    Jianping Wang
    Ranch Hand
    Posts: 60
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 383
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic