@Override public int propose(int value) { int myId = system.getProcessId(); boolean amIAlive; if (amIAlive = alives.isEmpty()) alives.put(myId, value); for (int neighbour : system.getProcessNeighbourhood()) system.sendMessage(neighbour, round, amIAlive ? value : null); while (awaitingMessages > 0) system.yield(); int numOfAlives = alives.size(); while (true) { if (alives.size() == 1) return alives.firstEntry().getValue(); round++; if (alives.size() > 0) numOfAlives = alives.size(); awaitingMessages = numOfAlives; if (!receivedForTheNextRounds.isEmpty()) for (Map.Entry<Integer, Integer> entry : receivedForTheNextRounds.removeFirst().entrySet()) { awaitingMessages--; if (entry.getValue() == null) alives.remove(entry.getKey()); else alives.put(entry.getKey(), entry.getValue()); } if (amIAlive) { awaitingMessages--; if (random.nextInt(numOfAlives) == 0) { alives.put(myId, value); for (int neighbour : system.getProcessNeighbourhood()) system.sendMessage(neighbour, round, value); } else { alives.remove(myId); for (int neighbour : system.getProcessNeighbourhood()) system.sendMessage(neighbour, round, null); } } while (awaitingMessages > 0) system.yield(); if (!alives.isEmpty() && !alives.containsKey(myId)) amIAlive = false; } }
@Override public int propose(int value) { if (this.value > value) { this.value = value; } receivedMsgs--; for (int neighbour : system.getProcessNeighbourhood()) system.sendMessage(neighbour, 0, value); while (receivedMsgs > 0) system.yield(); return this.value; }