Skip to content

Instantly share code, notes, and snippets.

@vaskoz
Created January 15, 2010 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaskoz/278103 to your computer and use it in GitHub Desktop.
Save vaskoz/278103 to your computer and use it in GitHub Desktop.
public class ThreadNinja {
private int rank;
public synchronized void fight(ThreadNinja enemy) {
if (!enemy.canDefend(this)) {
rank++;
}
}
public synchronized boolean canDefend(ThreadNinja enemy) {
if (this.rank > enemy.rank) {
this.rank++;
return true;
}
return false;
}
public static void main(String[] args) {
// write code here that will cause a deadlock in the methods above.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment