P
ppp
Hi, I try to implement Peterson algorithm in java. That is very easy.
Here you are:
class Peterson {
private int turn;
private boolean[] interested = {false, false};
public Peterson(String name) {
super(name);
}
public void enterRegion(int process) {
int other = 1 - process;
interested[process] = true;
turn = process;
while (turn==process && interested[other]==true)
;// do nothing
}
public void leaveRegion(int process) {
interested[process] = false;
}
}
But it's hard to me use this class any how.
Should i create 2 Threads, and in this threads method run should use
methods leave/enter Region?
please help.
Here you are:
class Peterson {
private int turn;
private boolean[] interested = {false, false};
public Peterson(String name) {
super(name);
}
public void enterRegion(int process) {
int other = 1 - process;
interested[process] = true;
turn = process;
while (turn==process && interested[other]==true)
;// do nothing
}
public void leaveRegion(int process) {
interested[process] = false;
}
}
But it's hard to me use this class any how.
Should i create 2 Threads, and in this threads method run should use
methods leave/enter Region?
please help.