Created
January 30, 2014 04:39
-
-
Save aerodame/8702736 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
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