aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes [URLyBird] Synchronization issues and null terminated fields problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "[URLyBird] Synchronization issues and null terminated fields problems" Watch "[URLyBird] Synchronization issues and null terminated fields problems" New topic
Author

[URLyBird] Synchronization issues and null terminated fields problems

Jean-Charles Wadja
Greenhorn

Joined: Aug 13, 2010
Posts: 5
Hi all,

I have started to work my assignment and after a week, I thought I had completed all the classes behind the DB interface. After reading a bit previous threads, I realize my mistakes, but I still have doubts about few things. I would really appreciate if you could help me clear them out.

1- Validity of the synchronization solution
Being new to synchronization, I went for what I thought was the easiest solution : using the synchronized key word as soon as I suspected thread concurrency could threaten the consistency of the data. This means that most of my methods uses it soon after their declaration. I am accessing the file with a RandomAccessFile object, and this is on this object that I am synchronizing on. The result of that is each operation is sequential, a thread trying to delete record 2 will have to wait until the thread modifying record 5 releases the lock on the RandomAccessFile object. I was happy with this, ready to document how slow this would be and possible solution, until I read somewhere that this (having a sequential access) would certainly cause me to fail. I cannot not find the thread anymore but I am pretty sure it was on this forum. Can you please confirm ?

The only alternative to this would be to lock the records individually, meaning that I would have to cache the data (which I did not, thinking that modifying the file directly was more solid/reliable).

2- Field are null terminated
I got confused by a paragraph in the assignment saying that the fields are "null terminated if less than the maximum length for the field". In the DB file provided, spaces are written where we should have seen null. So I took the decision to do the same and pad my fields with spaces as per below. Is this something you would recommand ?
tempField being the current String field, tempByteArray an array of the field expected size, and BLANK_BYTE = ' '

3- Is the value generated by the lock method the one that should be written on the last field of the record ? The assignment specify that this should be an 8 digit number, so I have limited the possible range for the cookie, starting from 10000000 to 99999999.

Thanks in advance. Please let me know if I am breaking any forum rule by posting the code above.

++

Sandrew
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5512
    
  13

Hi,

Welcome to the JavaRanch!

1/ you have to make sure your Data class will be thread-safe. And you'll have to implement a locking mechanism for your records. You could make your Data class thread-safe in different ways (marking your methods synchronized, use a synchronized block on some specific object,...). The locking mechanism for your records (purpose excellently described in ScjdFaq) should occur concurrently (like explained here). But it won't be automatic failure when threadB has to wait to lock record 5 (or delete record 6), because threadA is reading record 2. Because if you synchronize all your methods (like I did) only 1 thread can access a method of an instance at the same time (and all other threads will have to wait until this thread is done executing that method).

2/ nothing wrong with using spaces to pad the fields (I did it too).

3/ the customer id is a number (8 digits) that the CSR (user of your client application) has to enter. This has nothing to do with the generated lockCookie.

Kind regards,
Roel


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

Joined: Jul 23, 2010
Posts: 136
I might be a lil wrong in this but Another approach would be to use strings.
Then you can use String.format method to fit the data in the appropriate space.

It will be padded with whitespace ultimately. But it will be simpler to implement
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [URLyBird] Synchronization issues and null terminated fields problems