Skip to content

Instantly share code, notes, and snippets.

@aerodame
Created January 30, 2014 04:39
Show Gist options
  • Save aerodame/8702736 to your computer and use it in GitHub Desktop.
Save aerodame/8702736 to your computer and use it in GitHub Desktop.
public class Deadlock {
public Deadlock( ) {
Mutex mutex[] = new Mutex[4];
for ( int i = 0; i < 4; i++ )
mutex[i] = new Mutex( );
A threadA = new A( mutex );
B threadB = new B( mutex );
C threadC = new C( mutex );
threadA.start( );
threadB.start( );
threadC.start( );
}
public static void main( String arg[] ) {
Deadlock d = new Deadlock( );
}
class Mutex{ }
private class A extends Thread
{
private Mutex[] resource;
public A( Mutex[] m ) {
resource = m;
}
public void run( ) {
System.out.println( "A started" );
synchronized ( resource[1] ) {
System.out.println( "A got rsc 1" );
synchronized ( resource[0] ) {
System.out.println( "A got rsc 0" );
}
}
System.out.println( "A finished" );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment