aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Issue with multiple threaded delete access in lock manager Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Issue with multiple threaded delete access in lock manager" Watch "Issue with multiple threaded delete access in lock manager" New topic
Author

Issue with multiple threaded delete access in lock manager

Mark O' Sullivan
Ranch Hand

Joined: Aug 17, 2009
Posts: 160
Hi,
I'm currently having an issue with running multiple-threaded access for the delete method of assignment. The method works say 3 out of 4 times and then can enter deadlock. I will post code in hope someone spots a problem. I have 2 caches a valid and deleted record numbers cache. Below is my lock method.

This is probably fairly standard.
in delete method, just check is the record number passed locked by passed cookie

Anyone spot anything in my logic, that could be wrong? Any suggestions are very welcome, as one can imagine, multi-threaded issues are difficult to resolve!
Thanks.

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5597
    
  15

Hi Mark,

First a code remark:

Why don't make it a checkRecNo method which will throw RNFE when recNo is invalid. A lot less lines of code and code would become a lot easier to read (in my opinion).


I'm completely not familiar with the new locking api (used wait/notifyAll/synchronized), but it seems the isRecordNumberValid is a static method because RoomReservationFileAccess seems a class (if it is an object name, it should not start with capital letter). Are you sure these static methods are thread-safe too?
I guess with your logic is nothing wrong, because in my application I used similar logic:
a) check recNo
b) wait (if record is already locked)
c) check recNo
d) lock record by adding recNo to a map

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Mark O' Sullivan
Ranch Hand

Joined: Aug 17, 2009
Posts: 160
Hi,
Solved this issue. I was calling notify() instead of notifyAll() on the condition variable when unlocking. Damn, that was difficult to detect. Have take Roel's comments on board, and was already throwing the RNFE exception from the isRecordNumberValid function, just I'm new to Exception handling so I was creating unneccessary code in the lock function. Alls well again for Halloween time! Can go trick or treating!
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11525
    
100

Hey Mark

Why not have the RoomReservationFileAccess.isRecordNumberValid method throw the Exception if the record doesn't exist?

Note: there can be valid reasons for not doing this, just curious as to what your reasons are.


Doh - didn't see Roel's comment and your response. Should have known better than to post my own comments before reading his.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11525
    
100

Also - why not catch the InterruptedException in the outer try...finally block?

And - you are currently just swallowing the InterruptedException. I hope you plan to at least log it.
Mark O' Sullivan
Ranch Hand

Joined: Aug 17, 2009
Posts: 160
God, the great Andrew Monkhouse responded to one of my questions! Great day, appreciate the input and the SCJD book is great as well! Thanks!
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11525
    
100

Heh - you can blame my lack of responses on Roel and Roberto. When I see that they are taking an active interest in a topic I usually feel that there is little left for me to add.

Glad you like the book.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2268
    
    3

Andrew Monkhouse wrote:When I see that they are taking an active interest in a topic I usually feel that there is little left for me to add.


Honestly, I feel really honored to read this. Andrew, thanks for the words. It feels really good to know that we are able to help people, and it's also fun! We'll continue to help people, and I'm sure my good buddy Roel also thinks like me!


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5597
    
  15

Roberto Perillo wrote:and I'm sure my good buddy Roel also thinks like me!

Of course, you know what they say about great minds
Bernd Wollny
Ranch Hand

Joined: May 15, 2006
Posts: 59

Hi all,

the first call of RoomReservationFileAccess.checkRecNo(recNo); is it really necessary?
If the record is deleted no one has the ability to change it (or to lock it), if it doesn't exist there's no reason to wait for lock... So, why to do the first check?

Regards
Bernd


SCJP 1.4, SCJD/OCMJD
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5597
    
  15

Bernd Wollny wrote:the first call of RoomReservationFileAccess.checkRecNo(recNo); is it really necessary?

No, it isn't.

Kind regards,
Roel
Bernd Wollny
Ranch Hand

Joined: May 15, 2006
Posts: 59

Roel De Nijs wrote:
Bernd Wollny wrote:the first call of RoomReservationFileAccess.checkRecNo(recNo); is it really necessary?

No, it isn't.

That's what i wanted to hear, thanks!

Regards,
Bernd
Bernd Wollny
Ranch Hand

Joined: May 15, 2006
Posts: 59

Hi guys,

perhaps you have already noticed that i have the same locking strategy within my locking manager...
So, one follow-up question:
Due to safety reasons i have to make my check-record-no method thread-safe too....but, i was wondering if a read lock is enough or if i have to use a
write lock when checking it (it == my map storing record information)? What are your thoughts?

@Roel:
How did you handle the InteruptedException finally? I saw some post from you where you wrote that you are using

instead of wrapping it up into some user-defined RuntimeException, right? Was that your final decision or did you change before submitting to SUN?

Kind regards
Bernd
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5597
    
  15

Hi Bernd,

According to your 1st question: In my assignment I didn't use read and write locks, just synchronized the complete methods, so I'm not an expert in this matter. I would think a read lock is just fine, because check-record-no method would in my opinion just need to read to your map (and not write).

This thread is not about InterruptedException and how to handle it, so I'm not going to answer this question here because we should try to stick as much as possible to "1 thread for 1 question"-policy. So it becomes for other people trying to look for answers a lot easier to find what they are looking for. So feel free to post your question in a new thread or in the thread I started.

Kind regards,
Roel
 
 
subject: Issue with multiple threaded delete access in lock manager