+/EE360P
distributed systems
git clone https://git.julia.blue/+/EE360P
df69975
master
1class DiningMonitor implements Resource { 2int n =0 ; 3int state [] =null ; 4static final int thinking =0 ,hungry =1 ,eating =2 ; 5public DiningMonitor (int initN ) { 6n =initN ; 7state =new int [n ]; 8for (int i =0 ;i <n ;i ++)state [i ] =thinking ; 9 } 10int left (int i ) { 11return (n +i -1 ) %n ; 12 } 13int right (int i ) { 14return (i +1 ) %n ; 15 } 16public synchronized void acquire (int i ) { 17state [i ] =hungry ; 18test (i ); 19while (state [i ] !=eating ) 20Util .myWait (this ); 21 } 22public synchronized void release (int i ) { 23state [i ] =thinking ; 24test (left (i )); 25test (right (i )); 26 } 27void test (int i ) { 28if ((state [left (i )] !=eating ) && 29 (state [i ] ==hungry ) && 30 (state [right (i )] !=eating )) { 31state [i ] =eating ; 32notifyAll (); 33 } 34 } 35public static void main (String []args ) { 36DiningMonitor dm =new DiningMonitor (5 ); 37for (int i =0 ;i <5 ;i ++) 38new Philosopher (i ,dm ); 39 } 40}