Created
December 6, 2010 18:43
-
-
Save toddlipcon/730713 to your computer and use it in GitHub Desktop.
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.concurrent.atomic.AtomicBoolean; | |
public class Test { | |
public static void main(String args[]) { | |
final AtomicBoolean takenLock = new AtomicBoolean(); | |
final Object foo = new Object(); | |
Thread t = new Thread() { | |
public void run() { | |
synchronized (foo) { | |
while (true) { | |
takenLock.set(true); | |
try { | |
foo.wait(); | |
} catch (InterruptedException ie) { | |
Thread.currentThread().interrupt(); | |
} | |
} | |
} | |
} | |
}; | |
t.start(); | |
t.interrupt(); | |
System.err.println("Waiting for thread to take lock"); | |
while (!takenLock.get()) { Thread.yield(); } | |
System.err.println("Trying to take lock myself"); | |
synchronized (foo) { | |
System.err.println("got lock"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment