+/EE360P
distributed systems
git clone https://git.julia.blue/+/EE360P
df69975
master
1import java .util .Random ; 2public class MyThread extends Thread { 3int myId ; 4Lock lock ; 5Random r =new Random (); 6public MyThread (int id ,Lock lock ) { 7myId =id ; 8this .lock =lock ; 9 } 10void nonCriticalSection () { 11System .out .println (myId +" is not in CS" ); 12Util .mySleep (r .nextInt (1000 )); 13 } 14void CriticalSection () { 15System .out .println (myId +" is in CS *****" ); 16// critical section code 17Util .mySleep (r .nextInt (1000 )); 18 } 19public void run () { 20while (true ) { 21lock .requestCS (myId ); 22CriticalSection (); 23lock .releaseCS (myId ); 24nonCriticalSection (); 25 } 26 } 27public static void main (String []args )throws Exception { 28MyThread t []; 29int N =Integer .parseInt (args [0 ]); 30t =new MyThread [N ]; 31Lock lock =new Bakery (N );//or any other mutex algorithm 32for (int i =0 ;i <N ;i ++) { 33t [i ] =new MyThread (i ,lock ); 34t [i ].start (); 35 } 36 } 37}