我正在尝试在title中实施该算法,但目前无法正常运行:
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.这是ouptut:
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
看起来有时在他们的关键部分中有两个线程。 我也尝试过使用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
用于各种成员,但仍然无法正常工作。
答案 0 :(得分:4)
我担心的问题是你没有正确实现Peterson的算法。
具体来说,lock
方法中的外部循环需要从零开始,而不是从1开始。由于零是线程数的有效值,因此不能将其用作“默认”或“不是”在使用值“为水平数组(我已将FLAG和LAST数组重命名为维基百科对Peterson's algorithm的描述中使用的术语)。相反,为了这个目的,我将代码更改为使用-1。
最重要的是,您实施Peterson算法的这一部分
while last_to_enter[ℓ] = i and there exists k ≠ i, such that level[k] ≥ ℓ
wait
不正确。您的函数不会使用other_flags = other_flags && this.FLAG.get(k) >= l;
测试是否存在,因为如果数组中有一个元素,其中k&gt; = l不为真,则将other_flags设置为false。但逻辑应该是另一种方式。
我已将该部分重构为一个单独的方法并将其修复。
通过这些更改,它可以正常工作。内存屏障暗示为AtomicInteger
使用volatile变量,并且锁始终从上一个unlock
修改的AtomicInteger读取,因此它创建它发生在之前的关系中Java内存模型。
public class PetersonLockUnlock implements MutexInterface {
private AtomicInteger[] levels;
private AtomicInteger[] lastToEnter;
private int n;
public PetersonLockUnlock(int n) {
this.n = n;
this.levels = new AtomicInteger[n];
this.lastToEnter = new AtomicInteger[n];
for (int i = 0; i < n; i++) {
this.levels[i] = new AtomicInteger(-1);
this.lastToEnter[i] = new AtomicInteger(-1);
}
}
public void lock(int i) {
for (int l = 0; l < this.n - 1; l++) {
this.levels[i].set(l);
this.lastToEnter[l].set(i);
while (this.lastToEnter[l].get() == i && existsLevelGteL(l, i)) {
// busy-wait
}
}
}
private boolean existsLevelGteL(int l, int i) {
for (int k = 0; k < this.n; k++) {
if (k != i && this.levels[k].get() >= l) {
return true;
}
}
return false;
}
public void unlock(int i) {
this.levels[i].set(-1);
}
}
答案 1 :(得分:1)
我做了一些调整。每次都工作。似乎你的索引0有问题。
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(-1);
this.LAST[i] = new AtomicInteger(-1);
}
}
public void lock(int i) {
for (int l = 0; 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) {
other_flags = false;
for (int k = 0; k < this.N; k++) {
if (k == i) continue;
if (this.FLAG[k].get() >= l) {
other_flags = true;
break;
}
}
}
}
}
public void unlock(int i) {
this.FLAG[i].set(-1);
}