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