Created
July 26, 2012 07:55
-
-
Save jeffstyr/3180847 to your computer and use it in GitHub Desktop.
Held locks do not all show up in stacktraces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.util.concurrent.locks.*; | |
/* | |
javac ForgottenLockTest.java | |
java -cp . ForgottenLockTest | |
^\ | |
*/ | |
public class ForgottenLockTest { | |
public static void main(String[] args) | |
{ | |
System.out.println("started"); | |
final ReadWriteLock lock = new ReentrantReadWriteLock(); | |
lock.writeLock().lock(); | |
new Thread() { public void run() { | |
System.out.println("thread started"); | |
lock.writeLock().lock(); | |
System.out.println("thread done"); | |
} } .start(); | |
while(true) { | |
try { Thread.sleep(1000); } | |
catch( InterruptedException e ) { break; } | |
} | |
System.out.println("main done"); | |
} | |
} | |
/* | |
** Note that there is no indication that the "main" thread holds the lock that "Thread-1" is waiting on. | |
Full thread dump Java HotSpot(TM) 64-Bit Server VM (20.8-b03-424 mixed mode): | |
"Thread-1" prio=5 tid=1050c4800 nid=0x10d101000 waiting on condition [10d100000] | |
java.lang.Thread.State: WAITING (parking) | |
at sun.misc.Unsafe.park(Native Method) | |
- parking to wait for <7f310f5e0> (a java.util.concurrent.locks.ReentrantReadWriteLock$NonfairSync) | |
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:156) | |
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:811) | |
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:842) | |
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1178) | |
at java.util.concurrent.locks.ReentrantReadWriteLock$WriteLock.lock(ReentrantReadWriteLock.java:807) | |
at ForgottenLockTest$1.run(ForgottenLockTest.java:21) | |
"main" prio=5 tid=105000800 nid=0x100501000 waiting on condition [100500000] | |
java.lang.Thread.State: TIMED_WAITING (sleeping) | |
at java.lang.Thread.sleep(Native Method) | |
at ForgottenLockTest.main(ForgottenLockTest.java:26) | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.util.concurrent.locks.*; | |
/* | |
javac SingleThreadDeadlock.java | |
java -cp . SingleThreadDeadlock | |
^ \ | |
*/ | |
public class SingleThreadDeadlock { | |
public static void main(String[] args) { | |
System.out.println("starting"); | |
ReadWriteLock lock = new ReentrantReadWriteLock(); | |
lock.readLock().lock(); | |
lock.writeLock().lock(); | |
System.out.println("done"); | |
} | |
} | |
/* | |
Full thread dump Java HotSpot(TM) 64-Bit Server VM (20.8-b03-424 mixed mode): | |
"main" prio=5 tid=104000800 nid=0x100501000 waiting on condition [100500000] | |
java.lang.Thread.State: WAITING (parking) | |
at sun.misc.Unsafe.park(Native Method) | |
- parking to wait for <7f310f220> (a java.util.concurrent.locks.ReentrantReadWriteLock$NonfairSync) | |
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:156) | |
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:811) | |
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:842) | |
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1178) | |
at java.util.concurrent.locks.ReentrantReadWriteLock$WriteLock.lock(ReentrantReadWriteLock.java:807) | |
at SingleThreadDeadlock.main(SingleThreadDeadlock.java:17) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment