我正试图实现标题中Peterson算法的算法,但目前还没有正确地运行:
package me.fponzi.mutex;
import java.util.concurrent.atomic.AtomicInteger;
public class PetersonLockUnlock implements MutexInterface {
private AtomicInteger[] FLAG;
private AtomicInteger[] LAST;
private int N;
/**
* PetersonLockUnlock
*
* @param N number of processes.
*/
public PetersonLockUnlock(int N) {
this.N = N;
this.FLAG = new AtomicInteger[N];
this.LAST = new AtomicInteger[N];
for (int i = 0; i < N; i++) {
this.FLAG[i] = new AtomicInteger(0);
this.LAST[i] = new AtomicInteger(-1);
}
}
public void lock(int i) {
for (int l = 1; l < this.N-1; l++) {
this.FLAG[i].set(l);
this.LAST[l].set(i);
boolean other_flags = true;
while (other_flags && this.LAST[l].get() == i) {
for (int k = 0; k < this.N; k++) {
if (k == i) continue;
other_flags = other_flags && this.FLAG[k].get() >= l;
}
}
}
}
public void unlock(int i) {
this.FLAG[i].set(0);
}
}
这是主要的类:
import me.fponzi.mutex.MutexInterface;
import me.fponzi.mutex.PetersonLockUnlock;
public class Main {
static int test_value = 0;
public static class PrintThread implements Runnable{
private MutexInterface mutex;
PrintThread(MutexInterface m)
{
this.mutex = m;
}
@Override
public void run() {
String threadName = Thread.currentThread().getName();
int threadId = Integer.parseInt(threadName);
for (int i = 0; i < 5; i++)
{
mutex.lock(threadId);
test_value +=1;
mutex.unlock(threadId);
}
}
}
public static void main(String[] args) throws InterruptedException {
final int NTHREADS = 100;
PetersonLockUnlock p = new PetersonLockUnlock(NTHREADS);
Thread[] threads = new Thread[NTHREADS];
while (true) {
test_value = 0;
for (int i = 0; i < NTHREADS; i++) {
threads[i] = new Thread(new PrintThread(p), "" + i);
}
for (Thread t : threads) {
t.start();
}
for (Thread t : threads) {
t.join();
}
System.out.println("Result:" + test_value);
}
}
}
正如您所看到的,我创建了100个线程,它们都将test
变量增加了5次。因此期望值应该是500。这是输出结果:
Result:499
Result:500
Result:500
Result:500
Result:498
Result:499
Result:500
Result:499
Result:499
Result:500
Result:500
Result:500
Result:498
Result:500
Result:499
Result:500
Result:500
Result:499
Result:500
Result:500
Result:500
Result:500
Result:500
Result:500
Result:500
Result:500
Result:500
看起来有时候会有两个线程在它们的关键部分(critical section)内部。我也尝试过使用AtomicIntegerArray代替:
public class PetersonLockUnlock implements MutexInterface {
private AtomicIntegerArray FLAG;
private AtomicIntegerArray LAST;
private int N;
/**
* PetersonLockUnlock
*
* @param N number of processes.
*/
public PetersonLockUnlock(int N) {
this.N = N;
this.FLAG = new AtomicIntegerArray(N);
this.LAST = new AtomicIntegerArray(N);
for (int i = 0; i < N-1; i++) {
this.FLAG.set(i, 0);
this.LAST.set(i, 0);
}
}
public void lock(int i) {
for (int l = 1; l < this.N; l++) {
this.FLAG.set(i, l);
this.LAST.set(l, i);
boolean other_flags = true;
while (other_flags && this.LAST.get(l) == i) {
for (int k = 0; k < this.N; k++) {
if (k == i) continue;
other_flags = other_flags && this.FLAG.get(k) >= l;
}
}
}
}
public void unlock(int i) {
this.FLAG.set(i,0);
}
}
但仍然存在相同的问题。我也尝试使用 volatile
修饰各个成员,但仍然无效。
int l = 0; l < N-1
吗?另外,你不需要在第二个版本中显式地将数组设置为0。 - shmosel