考虑以下树:
我要做的是模拟树波算法,这样如果一个节点从其中一个直接连接的邻居收到一个令牌,它就会向该静默邻居发送一个令牌(对于一个叶子节点,它总是如此) )。如果节点从静默邻居收到令牌,则做出决定。节点总是7,树结构相同所以我总是知道每个节点的邻居(直连节点)。
树算法伪代码:
我有以下对象:
public final class Node implements Runnable {
private final int name;
// Indicating token receiving from neighbours
private Map<Node, Boolean> neigh = new
HashMap<Node, Boolean>();
public Node(int name) {
this.name = name;
}
public int getName() {
return name;
}
public void addNeigh(Node node) {
neigh.put(node, false);
}
private int flag() {
Collection<Boolean> c = neigh.values();
int count = 0;
for (Boolean bool : c) {
if (!bool) {
count++;
}
}
return count;
}
private Node getSilentNeigh() {
for (Entry<Node, Boolean> entry : neigh.entrySet()) {
if (false == entry.getValue()) {
return entry.getKey();
}
}
return null;
}
public void sendToken(Node from, String token) {
Node n;
if ((n = getSilentNeigh()) != null && flag() == 1) {
if (from.equals(n)) {
System.out.println(name + " decides!");
}
}
neigh.put(from, true);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Node)) {
return false;
}
final Node n = (Node) obj;
return name == n.name;
}
@Override
public int hashCode() {
int hc = 17;
return 37 * hc + name;
}
@Override
public void run() {
while(flag() > 1);
Node n = getSilentNeigh();
System.out.println(name + " sends token to " + n.getName());
n.sendToken(this, "token");
}
@Override
public String toString() {
return "Node " + name;
}
}
在run()方法中,有一个while(条件)实际上意味着..等待(从邻居接收令牌),当只有一个邻居时,节点没有收到令牌发送令牌给他。
这就是我创建节点的方式以及我如何相互关联:
// Number of nodes
int numberOfNodes = 7;
// Array of nodes
Node[] nodes = new Node[numberOfNodes];
for (int i = 0; i < nodes.length; i++) {
// Creating node
nodes[i] = new Node(i);
}
nodes[0].addNeigh(nodes[1]);
nodes[0].addNeigh(nodes[2]);
nodes[1].addNeigh(nodes[0]);
nodes[1].addNeigh(nodes[3]);
nodes[1].addNeigh(nodes[4]);
nodes[2].addNeigh(nodes[0]);
nodes[2].addNeigh(nodes[5]);
nodes[2].addNeigh(nodes[6]);
nodes[3].addNeigh(nodes[1]);
nodes[4].addNeigh(nodes[1]);
nodes[5].addNeigh(nodes[2]);
nodes[6].addNeigh(nodes[2]);
我所做的是随机选择要执行的节点的顺序:
// List holding random node numbers
List numbers = new ArrayList<Integer>();
int chosen = 0;
while (chosen < numberOfNodes) {
int processNum = randInt(0, (numberOfNodes - 1));
if (!numbers.contains(Integer.valueOf(processNum))) {
numbers.add(new Integer(processNum));
chosen++;
}
}
因此,例如节点可以按任何顺序:
0, 5, 3, 4, 6, 2, 1
5, 3, 0, 2, 1, 6, 4
3, 1, 0, 2, 4, 6, 5
然后我开始线程:
for (Integer number : numbers) {
Thread thread = new Thread(nodes[number]);
thread.start();
}
有时我会得到预期的结果(2必须决定):
Nodes selected: 0, 4, 5, 2, 6, 3, 1
5 sends token to 2
4 sends token to 1
6 sends token to 2
3 sends token to 1
1 sends token to 0
0 sends token to 2
2 decides!
2 sends token to 0
0 decides!
但通常我得到一个错误,只有一个决定:
Nodes selected: 5, 3, 4, 6, 0, 2, 1
3 sends token to 1
5 sends token to 2
4 sends token to 1
6 sends token to 2
2 sends token to 0
0 sends token to 1
1 decides!
Exception in thread "Thread-6" java.lang.NullPointerException
at uk.ac.ncl.csc8103.wave.Node.run(Node.java:86)
at java.lang.Thread.run(Thread.java:745)
是的,这是一个作业,我真的很接近这些家伙..但我面临这个问题。
答案 0 :(得分:1)
好吧,您的run
方法调用Node n = getSilentNeigh();
此调用可能会返回null,并且您使用n
变量而不检查它是否为空。
@Override
public void run() {
while(flag() > 1);
Node n = getSilentNeigh(); // this call can return null
System.out.println(name + " sends token to " + n.getName()); // you are probably getting
// NullPointerException here
n.sendToken(this, "token");
}
答案 1 :(得分:1)
Inside the run() method there is a while(condition) that actually means.. wait (receiving tokens from neighbours)
根据您的代码
,情况并非如此 while(flag() > 1);
如果Node只有一个Node(4/5/6) - 对于这些,flag()将返回1并且将在循环时终止,然后在发起者开始发送消息之前继续向前发送请求
<强>更新强>
如你所知,这个代码有4个启动器(3,4,5,6)并且除了while循环外...
拿这个
选择的节点:5,3,4,6,0,2,1 3将令牌发送给1 5将令牌发送到2 4将令牌发送到1 6将令牌发送到2 2将令牌发送到0 0将令牌发送到1 1决定!
这纯粹是一种竞争条件,如果先前启动的启动器(5)已经到达/发送消息到其他启动器的(3)邻居(1)。当Initator(3)尝试启动并获得SlientNeigh时,它将获得Null(没有slient neigh)。因此,您在n.sendToken处获得Null指针异常。
Reference for Algorithm:你不需要sycn(我们没有规则,每个节点只应该从邻居收到一条消息)。
从参考三个简单属性
– In each computation there is one initiator, which starts the algorithm by sending out exactly one message
–– A process, upon receipt of a message, either sends out one message or decides
–– The algorithm terminates in the initiator and when this happens, each process has sent a message at least once