我需要实现一个共识协议,利用一个带有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推入之前都会将自己的值写入proposed。
有人能否找出我的错误并指导我纠正代码?