// idea // turn = 1 iff turn[0]=turn[1] // turn = 0 iff turn[0] != turn[1] public class KesselAlgorithm { boolean wantCS[] = {false, false}; int turn[] = {0,0}; int local[] = {0,0}; public void requestCS0() { wantCS[0] = true; local[0] = turn[1]; turn[0] = local[0]; while (wantCS[1] && (local[0] == turn[1])) ; } public void releaseCS0() { wantCS[0] = false; } public void requestCS1() { wantCS[1] = true; local[1] = 1-turn[0]; turn[1] = local[1]; while (wantCS[0] && (local[1] != turn[0])) ; } public void releaseCS1() { wantCS[1] = false; } }