Forums Register Login

Could the static keyword cause a bottleneck/ deadlock?

+Pie Number of slices to send: Send
Hi,

I had decalred a variable of type HashMap using a static keyword.inside a singleton.



Once intialized multiple threads could be launched inside my application; and each thread was doing a get() on the hashMap for a different key (as was determined by the executing logic)



But I observed, that the multiple threads in my application soon become un-responsive..... sort of deadlocked.
The rest of the application is not affected, but the core feature (implemented in the threads) stops functioning.

I think a HashMap is safe to use in the above acenario as the multiple threads are not modifying it and only getting from it.

Could the 'static' keyword or the 'singleton' nature of the enclosing class be a culprit?

~g1

PS: For a reason, I cannot use java concurrency a.k.a ConcurrentHashMap.
+Pie Number of slices to send: Send
I've found a description of some problem with (maybe) similar symptoms:
Please, take a look at:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6423457

Especially, pay attention to:

Doug Lea writes:

"This is a classic symptom of an incorrectly synchronized use of
HashMap. Clearly, the submitters need to use a thread-safe
HashMap. If they upgraded to Java 5, they could just use
ConcurrentHashMap. If they can't do this yet, they can use
either the pre-JSR166 version, or better, the unofficial backport
as mentioned by Martin. If they can't do any of these, they can
use Hashtable or synchhronizedMap wrappers, and live with poorer
performance. In any case, it's not a JDK or JVM bug."

I agree that the presence of a corrupted data structure alone
does not indicate a bug in the JDK.



Adam
+Pie Number of slices to send: Send
Hello,

1) If you need to cache something at the singleton level why is your map declared static (anyway you should have only one class instance - singleton).
2) The singleton pattern it's not well implemented and it is not thread safe. Please review your singleton implementation using a static init or using a ThradLocal + double checked lock.
In your case I bet that multiple class instances are created (no singleton) and every instance write/read in your static map. Using a HashMap in a multithreaded environment will create deadlock (cycles on its internal structures)
+Pie Number of slices to send: Send
I guess there is no room for a deadlock here.





This works fine. No matter how many times you run it.
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2090 times.
Similar Threads
Referencing an object
Questions about the fixed "volatile" keyword
Singleton & multithread env.
HashMap access problem
Private Static
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 07:23:22.