一、简述
算法思想:双标志后检查法中,两个进程都争着想进入临界区,但是谁也不让谁,最后谁都无法进入临界区。Gary L. Peterson想到了一种方法,如果双方都争着想进入临界区,那可以让进程尝试“孔融让梨”,主动让对方先使用临界区。
Peterson算法用软件方法解决了进程互斥问题,遵循了空闲让进、忙则等待、有限等待三个原则,但是依然未遵循让权等待的原则。
二、代码实现
public class PeterSon implements Runnable {
private static int turn;
private static boolean[] flags = new boolean[2];
public static void main(String[] args) {
new Thread(new PeterSon(1), "T1").start();
new Thread(new PeterSon(0), "T0").start();
}
private final int thisId;
public PeterSon(int id) {
thisId = id;
}
public int otherId() {
return thisId == 0 ? 1 : 0;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 进入区
flags[thisId] = true;
turn = otherId();
while (flags[otherId()] && turn == otherId()) {
System.out.println(Thread.currentThread().getName() + " is waiting for thread scheduling.");
}
// 临界区
System.out.println(Thread.currentThread().getName() + " is in critical section.");
// 退出区
flags[thisId] = false;
// 剩余区
System.out.println(Thread.currentThread().getName() + " is in remainder section.");
}
}
}
三、运行结果
T0 is waiting for thread scheduling.
T1 is in critical section.
T1 is in remainder section.
T0 is waiting for thread scheduling.
T0 is in critical section.
T0 is in remainder section.
T0 is in critical section.
T1 is waiting for thread scheduling.
T1 is in critical section.
T1 is in remainder section.
T0 is in remainder section.
T1 is in critical section.
T1 is in remainder section.
T0 is waiting for thread scheduling.
T0 is in critical section.
T0 is in remainder section.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T0 is in critical section.
T0 is in remainder section.
T1 is waiting for thread scheduling.
T1 is in critical section.
T1 is in remainder section.
Process finished with exit code -1
T0 is waiting for thread scheduling.
T0 is in critical section.
T0 is in remainder section.
T1 is waiting for thread scheduling.
T1 is in critical section.
T1 is in remainder section.
T1 is in critical section.
T1 is in remainder section.
T0 is waiting for thread scheduling.
T0 is in critical section.
T0 is in remainder section.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T0 is in critical section.
T0 is in remainder section.
T1 is waiting for thread scheduling.
T1 is in critical section.
T1 is in remainder section.
Process finished with exit code -1
很明显我们可以看出运行结果中不会出现两个线程同时进入临界区的现象,也就是T0 is in critical section.\n T1 is in critical section.
的情况。