Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

create(String[ ] data)

 
Jared Chapman
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering if anyone could comment on my logic used in implementing create(String[] data).

This method never grabs a lock on a record. If Sun intended the use of locks in this record, then its argument list would include long lockCookie. Without locking records, the following is possible:
  • 2 threads call create at the same time
  • both threads find the same unused entry
  • thread #1 writes a record in the empty space
  • thread #2 still thinks the space is available, and overrites thread #1's data


  • I can think of two ways to solve this problem:

    1. Grab the lock of the static HashMap lockedRecords (uses the values Integer recNo, Long lockCookie as its key, value pairs). This will prevent all other threads from locking/unlocking any records . Incrementally check each record number in lockedRecords until a non-locked record number is found. When found, check if this record is valid. If it isn't valid, use this space to write the record. Otherwise, continue until successful. If no invalid records are found, write the record to the end of the database.

    OR

    2. Make all threads share 1 instance of Data, and synchronize the whole method.

    I realize that neither of these options are exactly performance friendly, but that apparently isn't a consideration with this program.

    Any thoughts?
     
    Inuka Vincit
    Ranch Hand
    Posts: 175
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Since you are using a single lock object isnt 1# and 2# basically the same. I used the second approach since I am using individual record locks.
     
    Jared Chapman
    Ranch Hand
    Posts: 81
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes they are basically the same in that all other threads will have to wait for the create method to end before locking/unlocking any new records. The difference being #1 can be used if each client has its own instance of Data, and #2 is used when all clients share 1 instance of Data. Actually, #1 can be used if all clients share 1 instance of data as well.

    So I guess my question becomes: should I provide each client with their own instance of Data, or make all clients share 1 instance of data and synchronize the data-altering methods?
     
    Inuka Vincit
    Ranch Hand
    Posts: 175
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you do provide each client with an instance of Data, wont the locks be redundant since ony one client access the file at a time?(well only once client acccess the object)

    Besides that if each of the Data objects has a file stream then how are you going to synchronize the file access. I am not realy sure what happens during concurrent file access when there are multiple file handles. I would think the transactions are queued but not sure.

    What your aiming for will work but I am not sure if it will make a good design.
     
    Jared Chapman
    Ranch Hand
    Posts: 81
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you do provide each client with an instance of Data, wont the locks be redundant since ony one client access the file at a time?


    Even if only one client can access the database file at a time, the records still need to be locked, because the operations of the Data class are not atomic (is that the correct term?).

    For example, to update a record, you:
    (1) read the file to see if the record is valid
    (2) (assuming it's a valid record) write the updated data to the file

    What happens if someone deletes the file between (1) and (2)? I'm not accessing the file between (1) and (2), only during (1) and during (2). But by locking the record first, I'm preventing another client from manipulating the data while I am between (1) and (2).

    Besides that if each of the Data objects has a file stream then how are you going to synchronize the file access. I am not realy sure what happens during concurrent file access when there are multiple file handles. I would think the transactions are queued but not sure.

    That I still need to think about more.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic