I user sun jdk 1.5 ThreadPoolExecutor( 24, 24,60,TimeUnit.SECONDS, new LinkedBlockingQueue()). soemtime I use jdb tool to find the status of all threads in thread pool are " waiting in a monitor", the code is :
String key = getKey(dt.getPrefix(), id);
synchronized (key.intern()) { ----->
Is there a problem in "synchronized (key.intern()) " ?
I get following informatnio using jdb tool, the status of 24 threads is "waiting in a monitor", it means 24 threads are deadlock at "key.intern()".
(java.lang.Thread)0x28 pool-3-thread-2 waiting in a monitor
(java.lang.Thread)0x27 pool-3-thread-3 waiting in a monitor
(java.lang.Thread)0x1b pool-3-thread-4 waiting in a monitor
(java.lang.Thread)0x1a pool-3-thread-5 waiting in a monitor
(java.lang.Thread)0x19 pool-3-thread-6 waiting in a monitor
(java.lang.Thread)0x18 pool-3-thread-7 waiting in a monitor
(java.lang.Thread)0x17 pool-3-thread-8 waiting in a monitor ...
so the result is : in multi-threads environment, Sting intern() method may be deadlock, ok ?
I posted a related question to this once that you might want to take a look at: Problem with synchronizing on String objects?
What I learned was: using intern'ed Strings for synchronization is a bad practice.
Quite. The problem is that key.intern() isn’t really that unique because it’s returning a string from a pool. String.intern() might return the same object even when used on different objects. Try using key
itself or a different object altogether.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With