SCJP 1.4, SCJD
----------------<br />SCJP (98%)<br />SCJD (81%)<br />SCEA (near future)
Originally posted by Frank Verbruggen:
BTW this is not a deadlock, but it is a client waiting for a very long time.
What does this scenario do if notifyAll() is used instead of notify() ?
Frank
SCJP 1.4, SCJD
----------------<br />SCJP (98%)<br />SCJD (81%)<br />SCEA (near future)
Current Status:<br /> <br />SCJP 1.4<br />SCJD (in progress)
SCJP 1.4, SCJD
SCJP1.2 ,SCJD1.5 , SCBCD1.3 ,SCWCD1.4
Originally posted by David Abramowicz:
I have not come across any reason for calling notify rather than notifyAll, other than perhaps performance. But I believe that ESPECIALLY in this case that overhead is negligable (probably most other case also).
Therefore, I guess if you want to be on the safe side... notifyAll is the way forward.
SCJP 1.4, SCJD
Originally posted by mark ken:
hello Frans:
Did you use hashtable to store the locked record, and use cookie to identify the locked record. Or, you just add a flag varible in record object to declare this record has been locked. just change this flag to unlock this record.
SCJP 1.4, SCJD
Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4 all those years ago...)
SCJP1.2 ,SCJD1.5 , SCBCD1.3 ,SCWCD1.4
Originally posted by Jeff Bosch:
You could always fix it then pay $400 to resubmit...
SCJP 1.4, SCJD
MOON -- SCJP1.2
Originally posted by Jesse Xie Y.S.:
My opinion is
1. use notifyAll() instead of notify()
The reason is
a) notify() can only wake up a waiting thread. The sample is just the same.
b)notify() can only wake up a waiting thread randomlly
client 0 locks record Y
client A locks record X
client B locks record X; is put in wait()
client C locks record Y; is put in wait() too
client A deletes record X
client A unlocks record X; notify() is called
client C (Not B) wakes up to find that record Y is still locked;
Then we have a unlucky-lock: B!
SCJP 1.4, SCJD
--------<br />Andy Zhu<br />scjp 1.4<br />scjd 1.4<br />SAS Certified Programmer 9.0
Originally posted by Andy Zhu:
Hey, Frans:
an awesome idea on record-level locking. I am interested in this case: what object do you lock on when you createRecord if you have one record lock object per record (hope I don't understand you wrong)? Mainly, client a and b try to create same record; since that record has not been created/is creating, what will be the locker object for this case?
SCJP 1.4, SCJD
For create I indeed had to make an exception, because locking on a record that does not yet exist is of course impossible. In that case I synchronized on my LockCollection class (which is the singleton collection of all the RecordLock objects).
--------<br />Andy Zhu<br />scjp 1.4<br />scjd 1.4<br />SAS Certified Programmer 9.0
Originally posted by Andy Zhu:
[QBIn my implementation, I used one locker object, which allows minimum worry for data lock. More than one locker objects imposes the potential danger. It is still doable, but be careful. In particular, for a non create, do you need to lock on 2 objects in your record-level locking?[/QB]
SCJP 1.4, SCJD
When getting a specific RecordLock, the code is synchronized on both the LockCollection and the specific RecordLock object.
Although looking back at my code now, I don't think the synchronization on the LockCollection is even required in that situation, because the only change to the collection can be the creation of new records.
... with same order, there is no risk of a deadlock there.
--------<br />Andy Zhu<br />scjp 1.4<br />scjd 1.4<br />SAS Certified Programmer 9.0
Originally posted by Andy Zhu:
But this is to eliminate your effort of record locking. See:
lock LockCollection -> lock RecordLock -> do something -> unlock RecordLock -> unlock LockCollection
is equivalent to:
lock LockCollection -> do something -> unlock LockCollection.
How about deleteRecord? It should modify the LockCollection.
SCJP 1.4, SCJD
Originally posted by David Harkness:
I only know as much about this scenario as I have been able to get from the discussion itself, but another method to block two Threads from creating new records at the same time would be to have a single "creating new record RecordLock" in the LockCollection. This way you don't lock the collection, blocking other Threads from operating on other records.
SCJP 1.4, SCJD
If the requirement is to ensure that only one thread can be creating a new record, my proposal would be to create a single RecordLock instance for locking instead of locking on the collection.Originally posted by Frans Janssen:
I am not sure what you mean with "single". Do you mean a single synchronized method? Because that is exactly what I did. But it does block the collection during the creation of the record.
Originally posted by David Harkness:
From your description of creating new records, I assume you have this:Is that correct? If so, then while thread A is creating a new record, no other thread can modify or delete any other records. This problem is avoided if you change the second locking mechanism to use this single shared RecordLock instance for new records:
SCJP 1.4, SCJD
Where does a nanny get ground to air missles? Protect this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|