The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
if modify() is synchronized and Data class has only one instance for all clients, wont it be like below:
for all clents who want to write, even to different recorders, only one client can write at one time.
wont the perfomance be too slow?
Also, if synchronized modify() and only one instance of Data class, since modify() is already synchronized, and only one client can get this mehtod as an atomic block at one time, it seems it's not necessary to implement lock/unlock at all. it has already been thread safe, dosent it?
Andrew: "I think you will find it easier to have one instance of the Data class. If you have more than one, you will have problems with the add method."
Paul: the client adding a new record needs to get database lock, and the other clients will wait() for it. is there any possible problems?
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
you think that the basic reason why lock/unlock are needed is that when 2 clients try to update the same recorder, but the 1st one update the seat nubmer to 0, the 2nd one should be notified that the number is 0
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
at first, my idea was to use a static lock variable in the Data class. unfortunately, it seems that dose not work because the wait() object can not be notified by using the static lock.would you agree with this?
is the correct way to use trace the client id, for example, putting the current thread name into a data structure in the lockManager() class?
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
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:
Perhaps it might help to think about a real world equivalent: several people all going to the one clerk and asking for locks. Since the clerk can only deal with one person at a time, you cannot have two people asking for the same lock simultaneously. If the lock has not already been granted, the clerk approves the request and writes down somewhere which locks have been granted. If the lock has already been granted, the clerk tells the requesting person to wait. Those people who have had their requests granted can go off and work.
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 Paul
I don't understand why you can't have the static lock in the Data class either. As far as I can tell, it should work.
When you say that the wait() thread cannot be notified - what happens? Do you get an exception? Or does it appear to work but nothing happen?
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:
Personally I don't like locking the entire database just to create a new record, however I do acknowledge that it may be easier to code than my solution.
The problems with the create method and having multiple Data classes is:how do you ensure that all other instances of the Data class agree with the number of records in the database? how do you ensure that only one instance of the Data class is calling the create() method at any given time?
The second issue is handled by the fact that you are locking the entire database. The first can also be handled simply. (To be honest, I did have multuple instances of Data class in my submission, and I handled these issues, but I wish I hadnt gone with the multuple instances of Data class). However, the instructions do state that the provided classes are complete with the exception of three methods, so I dont think we should be modifying the Data class.
for all requests there is only a single instance of Data class which uses the LockManager.
Andrew: how do you ensure that only one instance of the Data class is calling the create() method at any given time?
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:
It still leaves the other issue though: how to ensure that all instances of the Data class agree about how many records exist in the database. To fix this, you have to make a change to the Data class.