• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Could the static keyword cause a bottleneck/ deadlock?

Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?


PS: For a reason, I cannot use java concurrency a.k.a ConcurrentHashMap.
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've found a description of some problem with (maybe) similar symptoms:
Please, take a look at:


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.

Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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)
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess there is no room for a deadlock here.

This works fine. No matter how many times you run it.
    Bookmark Topic Watch Topic
  • New Topic