Concurrency 我从一致数较少的对象构造了一致数为n的对象,什么';错在哪里?
这个问题是在我试图解决《多处理机编程的艺术》练习68时提出的。 给定n个寄存器具有Concurrency 我从一致数较少的对象构造了一致数为n的对象,什么';错在哪里?,concurrency,synchronization,atomic,consensus,Concurrency,Synchronization,Atomic,Consensus,这个问题是在我试图解决《多处理机编程的艺术》练习68时提出的。 给定n个寄存器具有读取/写入/获取和增量,所有寄存器的一致数均不大于2 首先,只使用enq和first实现一个队列: class Queue { AtomicInteger head = new AtomicInteger(0); AtomicReference items[] = new AtomicReference[Integer.MAX_VALUE]; void enq(Object x){ in
读取
/写入
/获取和增量
,所有寄存器的一致数均不大于2
首先,只使用enq
和first
实现一个队列:
class Queue {
AtomicInteger head = new AtomicInteger(0);
AtomicReference items[] =
new AtomicReference[Integer.MAX_VALUE];
void enq(Object x){
int slot = head.getAndIncrement();
items[slot] = x;
}
Object first() {
return items[0];
}
}
然后,我可以使用此队列为任意n个线程实现decision
:
class Consensus {
protected T[] proposed = (T[]) new Object[N];
private Queue q = new Queue;
public Object decide(Object value) {
int i = ThreadID.get();
proposed[i] = value;
q.enq(i);
int j = q.first();
return proposed[j];
}
}
我不明白问题出在哪里