• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

WeakHashMap (for locking records) Issues

 
Sudhansu Pati
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
This is surprising to me. But WeakHashMap solution didn't work for me. I used WeakHashMap for storing locked records. When i was testing with 3-6 threads, I didn't see any issues.
But today I tried to test my final aplication with about 40 concurrent threads (15 for book, 15 for release, 5 for create and 5 for delete). I found that WeakHashMap occasionally deleted the entries from itself. So there were situations when Record 1 is locked and updated by thread A, but WeakHashMap deleted that entry (Record 1). So another Thread B was able to lock it whiile it is already locked by A. Now when Thread A tried to unlock it, the application was throwing an exception.
Again this issue is not repeatable. It happens only sometimes. For example, with 40 simultaneous threads, the application failed 3 out of 10 times.
I am not sure how the entries of WeakHashMap are managed by the JVM. But looks like I am done with it and back to HashMap.
The problem with HashMap is, there may be situations when a record is locked for ever. To make sure those situations don't arise, I have to check every catch block and unlock the locked records.
Regards,
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Suds,
What were you using as the key in your WeakHashMap?
Regards, Andrew
 
Stephen Galbraith
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
this sounds like garbage collection and not maintaining a reference to the key other tha in the WeakHashMap (it caught me out too!)
If you call System.gc() {I know this only "suggests" that the garbage collector is run} then is your problem more repeatable?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic