我需要实现一个使用带有 peek() 方法的队列的共识协议,以表明可以为任意数量的线程达成共识,即带有 peek() 方法的队列具有无限的共识数
这是我的代码
import java.util.LinkedList;
import java.util.Queue;
public class PeekConsensus extends ConsensusProtocol<Integer>
{
Queue<Integer> queue ;
public PeekConsensus(int threadCount)
{
super(threadCount); //threadCount is a command line argument from the main class specifying the number of threads to handle this process
queue = new LinkedList<Integer>() //FIFO queue
}
public Integer decide(Integer value)
{
this.propose(value); // stores "value" in a vector named proposed, at index ThreadID.get()
int i = ThreadID.get() ;
queue.add(i)
System.out.println("Thread " + i + " : " + i) ; // Required by specifications to print thread id and the value added when an item is enqueued
System.out.println("Thread " + i + " : " + queue.peek()) ; // Required by specifications to print thread id and the value of peek() when when peek() is called
return proposed.elementAt(queue.peek()) ;
}
}
据我了解,这应该可行,因为如果两个线程返回不同的值, peek() 将必须返回不同的线程 id 并确保有效性,因为每个线程在推送其线程 id 之前将其自己的值写入建议中。
有没有人能够弄清楚我哪里出错并指导我纠正我的代码