Skip to content

Instantly share code, notes, and snippets.

@toddlipcon
Created December 6, 2010 18:43
Show Gist options
  • Save toddlipcon/730713 to your computer and use it in GitHub Desktop.
Save toddlipcon/730713 to your computer and use it in GitHub Desktop.
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