Originally posted by Jesse Xie Y.S.:
5. Make sure that the process of checking if a record is locked by other thread or checking if the record is exist, before locked a record, should be keep in synchronized block.
Yep. Alternatively you could lock the record regardless of whether it is a valid number or not, then afterwards confirm that it is valid and unlock the record if it is not valid. Personally I think your solution is going to be a little neater / more efficient.I’m thinking possibly need a synchronize in lock() as between read of record to make sure it exists and call to LockManager.lock() thread could be swapped out.
Well you don't need to synchronize the entire method, but you will need synchronization around the calls to the file access methods.And I’m thinking in update() I don’t need a synchronize cause the first check is validateLock so if it does not have access an Exception is thrown.
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Originally posted by Andrew Monkhouse:
Well you don't need to synchronize the entire method, but you will need synchronization around the calls to the file access methods.
Originally posted by Conor O'Mahony:
I synchronized all places where my Random Access File was accessed. Does this sound reasonable ?
Yep - you do need to synchronize either the method or the block around the {seek .. read}.So lets take read(..) as an example.
It involves calls to seek, skipBytes and read.
If two threads were to call read() and the process went as follows:
Thread1 calls seek
Thread1 calls skipBytes
Thread1 swapped out
Thread2 calls seek
Thread1 calls read
Thread1 is now reading in the wrong position in the RAF.
If this is the case then all of the unsynchronzied methods mentioned above need to be synchronized. (or at least the blocks dealing with the RAF)
Am I reading this situation right ?
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
For the record - I believe that discussion of handling orphaned locks / locks held too long is outside of scope for the assignment.Just saw a mention of a timeout on a lock in another topic.
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Originally posted by Andrew Monkhouse:
Hi Alan Yep - you do need to synchronize either the method or the block around the {seek .. read}.
Taking that a step further though - what else is that method doing apart from the two method calls that you identified as having potential thread conflicts?
I think you might be validating the record number prior to the disk I/O. You might be checking whether the record read is deleted or not. And you might be converting from the bytes read into a String[]. You might be doing other things as well. Do any of these extra steps suffer from the same potential thread conflicts? If not, do they need to be inside a synchronized block?
So - in the argument between synchronizing a method, and synchronizing a block within that method - can you see which one I would lean towards?
Regards, Andrew
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Originally posted by Andrew Monkhouse:
Hi Alan Yep - you do need to synchronize either the method or the block around the {seek .. read}.
Taking that a step further though - what else is that method doing apart from the two method calls that you identified as having potential thread conflicts?
I think you might be validating the record number prior to the disk I/O. You might be checking whether the record read is deleted or not. And you might be converting from the bytes read into a String[]. You might be doing other things as well. Do any of these extra steps suffer from the same potential thread conflicts? If not, do they need to be inside a synchronized block?
So - in the argument between synchronizing a method, and synchronizing a block within that method - can you see which one I would lean towards?
Regards, Andrew
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog