aspose file tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes B&S Threads wating on a deleted Record Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Reply Bookmark "B&S Threads wating on a deleted Record" Watch "B&S Threads wating on a deleted Record" New topic
Author

B&S Threads wating on a deleted Record

Jim Petersson
Ranch Hand

Joined: May 28, 2008
Posts: 48
Hi,
I would like to get som input on how to handle the following scenario:

- Thread 1 locks record 5
- Thread 2 tries to lock record 5 (goes into waiting state)
- Thread 3 tries to lock record 5 (goes into waiting state)
- Thread 1 deletes record 5

Even if the delete-method would unlock and notify on record 5, only one of Thread 2/3 would wake up, and the other one would be waiting for ever.

How have you handled this?

Thanks, Jim


SCJP 5<br />SCJD
Joshua Fix
Ranch Hand

Joined: Sep 18, 2007
Posts: 57
I haven't tested this portion of my code yet, but using traditional wait and notify methods, this is how I plan to do it:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#wait(long)


SCJP 5.0
Joe O'Keefe
Greenhorn

Joined: Oct 01, 2007
Posts: 4
One way you can solve this is to have a List/ArrayList that holds references to all of the records that have been deleted. Then before you start to wait on the record (when locking it), do a check to see if it's in this list. Also, once you do get the lock for that record (and finish waiting), check again to see if it's deleted. In both cases you can throw a RecordNotFoundException if it's deleted.
That way if one thread deletes a record, and another thread is still waiting to get the lock on it, the other thread will see it's deleted and just throw an exception. This solution would go together with reusing deleted records though.
Zeng Wei Chu
Greenhorn

Joined: Dec 05, 2007
Posts: 27
Hi, the way i implemented my solution for this problem is that i always do a check after locking on a particular record. if the record happens to be deleted, i unlock the record and throws a RecordNotFoundException.


SCJP | SCJD | SCWCD | SCBCD | SCEA
Jim Petersson
Ranch Hand

Joined: May 28, 2008
Posts: 48
The way I decided to implement it is to have the delete method unlock the record after it has been deleted (and call notifyAll on it). And my lock method checks so that the record still exists after it has received a lock for it. (If it doesn't exist, I throw a RecordNotFoundException)

Thanks for you input
Soroj Margun
Ranch Hand

Joined: Jun 15, 2008
Posts: 44
With my B&S exam, I use the same concept as post in replies above. When user delete record, data manager also set flag status on the lock and notify all waiting thread. With this concept, I wont have any deleted list to maintain. But on the others hand, if you maintain the deleted record, it may be easier to create a new record because you will know which record is ready to reuse


SCJP 1.2; SCWCD 1.2,1.4; SCBCD 1.3; SCJD 5.0
 
jQuery in Action, 2nd edition
 
subject: B&S Threads wating on a deleted Record
 
Similar Threads
NX: Backing out of RMI operation
Threads 001
Thread states, notify, notifyAll, wait
notifyall
NX: lock(Data data, int recNo) allowing multiple locks of one record!! why???