aspose file tools
The moose likes Threads and Synchronization and the fly likes Could the static keyword cause a bottleneck/ deadlock? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Could the static keyword cause a bottleneck/ deadlock?" Watch "Could the static keyword cause a bottleneck/ deadlock?" New topic
Author

Could the static keyword cause a bottleneck/ deadlock?

Jeevan Sunkersett
Ranch Hand

Joined: Jul 03, 2007
Posts: 71
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.
Adam Smolnik
Ranch Hand

Joined: Apr 15, 2009
Posts: 63
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


SCJP, SCWCD, SCBCD, SCDJWS
Andrei Matyas
Greenhorn

Joined: Apr 15, 2007
Posts: 24
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)
abhay bansal
Greenhorn

Joined: Nov 26, 2009
Posts: 8
I guess there is no room for a deadlock here.





This works fine. No matter how many times you run it.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel
 
subject: Could the static keyword cause a bottleneck/ deadlock?
 
Similar Threads
HashMap access problem
Singleton & multithread env.
Questions about the fixed "volatile" keyword
Private Static
Referencing an object