Java – deadlock on synchronized (string intern())
My user sun JDK 1.5 ThreadPoolExecutor (24,24,60, timeunit.seconds, new linkedblockingqueue()) Soemtime I use the JDB tool to find that the status of all threads in the thread pool is "waiting in the monitor". The code is:
String key = getKey(dt.getPrefix(),id); synchronized (key.intern()) { ----->
Is there a problem in "synchronized (key. Intern())?"?
I use the JDB tool to obtain the following information. The status of 24 threads is "waiting in the monitor", which means that 24 threads are in a deadlock state in "key. Intern()"
(Java. Lang. thread) 0x28 pool-3-thread-2 waiting in the monitor
(Java. Lang. thread) 0x27 pool-3-thread-3 waiting in monitor
(Java. Lang. thread) 0x1b pool-3-thread-4 waiting in the monitor
(Java. Lang. thread) 0x1a pool-3-thread-5 wait in the monitor
(Java. Lang. thread) 0x19 pool-3-thread-6 wait in the monitor
(Java. Lang. thread) 0x18 pool-3-thread-7 waiting in the monitor
(Java. Lang. thread) 0x17 pool-3-thread-8 waiting in the monitor
So the result is: in a multithreaded environment, the sting Intern () method may deadlock, okay?
Solution
I once sent a related question. You may want to see: problem with synchronizing on string objects?
What I've learned is that using inter'ed strings for synchronization is a bad practice