Replaced the PriorityQueue by the PriorityBlockingQueue in the LatencyNodeProvider class due to the fact that multiple threads can access the same provider

This commit is contained in:
Nelson R. Perez 2018-11-08 17:59:01 -05:00
parent b6aab142b6
commit af5896a160

View file

@ -2,18 +2,15 @@ package cy.agorise.graphenej.network;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.PriorityQueue;
import java.util.concurrent.PriorityBlockingQueue;
public class LatencyNodeProvider implements NodeProvider {
private HashSet<String> mRemovedNodeURLs;
private PriorityQueue<FullNode> mFullNodeHeap;
private PriorityBlockingQueue<FullNode> mFullNodeHeap;
public LatencyNodeProvider(){
mFullNodeHeap = new PriorityQueue<>();
mRemovedNodeURLs = new HashSet<>();
mFullNodeHeap = new PriorityBlockingQueue<>();
}
@Override
@ -24,13 +21,12 @@ public class LatencyNodeProvider implements NodeProvider {
@Override
public void addNode(FullNode fullNode) {
mFullNodeHeap.add(fullNode);
mRemovedNodeURLs.remove(fullNode.getUrl());
}
@Override
public boolean updateNode(FullNode fullNode) {
if (!mRemovedNodeURLs.contains(fullNode.getUrl())) {
mFullNodeHeap.remove(fullNode);
boolean existed = mFullNodeHeap.remove(fullNode);
if(existed){
return mFullNodeHeap.offer(fullNode);
}
return false;
@ -44,19 +40,16 @@ public class LatencyNodeProvider implements NodeProvider {
* @return True if the node priority was updated successfully
*/
public boolean updateNode(FullNode fullNode, int latency){
if(!mRemovedNodeURLs.contains(fullNode.getUrl()) && mFullNodeHeap.remove(fullNode)){
fullNode.addLatencyValue(latency);
boolean existed = mFullNodeHeap.remove(fullNode);
if(existed){
return mFullNodeHeap.add(fullNode);
}else{
return false;
}
return false;
}
@Override
public void removeNode(FullNode fullNode) {
if (mFullNodeHeap.remove(fullNode)) {
mRemovedNodeURLs.add(fullNode.getUrl());
}
mFullNodeHeap.remove(fullNode);
}
@Override