Vectors are synchronized classes. Are you sure you want to use a synchronized class within a synchronized method? What benefit will it give you?Have a static Vector that stores record numbers that are locked. Synchronize on this Vector before adding or removing a record number from it.
Playing devil's advocate here: if you did lock the entire database, you could be certain that the records are correct at the moment you read them, however milleseconds after you return the records to the client, they could be out of date due to deletions / bookings / creations. So what does it really gain you?Do I need to use locking for read operations as well as write operations? [...] If I did not use locking (as this is a read operation), I could calculate the number of records, only for this to change whilst I am reading them (for example one is deleted and then RecordNotFound is thrown). If I did use locking, then surely I would need to lock the entire database, as the number of records needs to be the same during the algorithm?
You might want to check your instructions. If your instructions tell you that your Data class must implement locking, at that that locking must provide certain functionality, then that is what your Data class (or a class it calls) must do.And lastly do I need to implement locking in Data.java or can I use an adapter class (like Max's book does?).
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Vectors are synchronized classes. Are you sure you want to use a synchronized class within a synchronized method? What benefit will it give you?
Playing devil's advocate here: if you did lock the entire database, you could be certain that the records are correct at the moment you read them, however milleseconds after you return the records to the client, they could be out of date due to deletions / bookings / creations. So what does it really gain you?
Also: locking the entire database means that no other client would be able to work on the database. If you are doing this for reading the entire database and/or find methods then you could be potentially blocking multiple clients for long periods of time. Does this fit with the requirement that "Your server must be capable of handling multiple concurrent requests"? (my highlighting)
You might want to check your instructions. If your instructions tell you that your Data class must implement locking, at that that locking must provide certain functionality, then that is what your Data class (or a class it calls) must do.
Note that the Data class can defer to another class if you want it to (so your Data class itself could be an Adapter or a Facade).
Max's book has its own project, with its own requirements - these are not always a perfect match for the Sun assignment (and this was actually a design decision in developing the book: having the book's project and the Sun assignment too similar is "a bad thing�". In addition, the book was written at a time when the "Fly By Night Services" (FBNS) assignment was in vogue, and it had different requirements than URLyBird (and FBNS allowed an adapter class for locking purposes).
Assuming you don't lock reads, the effects are undefined. Most of the time you will probably be lucky and get the right value back, simply because it is unlikely that the thread swapping will happen at a critical point. The problem is that it is "unlikely" not "impossible" - if the thread reading from the collection swapped out during the read and the collection was added to or deleted from then you might get the wrong value back. Or you might get a NullPointerException. Or, depending on the collection and how it is implemented, you might get a ConcurrentModificationException. No way to tell - this would be a mistake.
Originally posted by Andrew Monkhouse:
Vectors are synchronized classes. Are you sure you want to use a synchronized class within a synchronized method? What benefit will it give you?
Originally posted by Steve Smith:
As I understand it, the fact that Vector is synchronized just means that the methods are atomic and cant be interrupted. There is still the possibility for it being interrupted between methods. For example if I read the vector, then write to it, it can still be interrupted between these two methods.
So I think I do need to synchronize on the collection, but perhaps I can use an ArrayList instead of a Vector and it would still be thread safe? The disadvantage I think would then be that, for example, the vector could be read whilst in the middle of being written to (assuming I don't lock reads). I am not sure what effect this would have.
AgreedOriginally posted by Steve Smith:
One concern is how to handle a RecordNotFoundException when I read all records. [...] I suppose really this is a different topic to locking though.
Originally posted by Steve Smith:
And lastly do I need to implement locking in Data.java or can I use an adapter class (like Max's book does?).
Originally posted by Andrew Monkhouse:
You might want to check your instructions. If your instructions tell you that your Data class must implement locking, at that that locking must provide certain functionality, then that is what your Data class (or a class it calls) must do.
Note that the Data class can defer to another class if you want it to (so your Data class itself could be an Adapter or a Facade).
Originally posted by Steve Smith:
My main concern is that automated testing of Data.java will be performed and therefore I can't use a "higher level" class to manage locks.
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|