List of usage examples for java.util TreeMap notifyAll
@HotSpotIntrinsicCandidate public final native void notifyAll();
From source file:com.almende.dht.rpc.DHT.java
/** * Iterative_node_lookup.//from w w w.j a va2s . co m * * @param near * the near * @return the list */ public List<Node> iterative_node_lookup(final Key near) { final TreeMap<Key, Node> shortList = new TreeMap<Key, Node>(); final Set<Node> tried = new HashSet<Node>(); insert(shortList, near, rt.getClosestNodes(near, Constants.A)); final int[] noInFlight = new int[1]; noInFlight[0] = 0; boolean keepGoing = true; while (keepGoing) { while (noInFlight[0] > 0) { synchronized (shortList) { try { shortList.wait(100); } catch (InterruptedException e) { } } } List<Node> copy = Arrays.asList(shortList.values().toArray(new Node[0])); Collections.sort(copy); final Iterator<Node> iter = copy.iterator(); int count = 0; while (iter.hasNext() && count < Constants.A) { final Node next = iter.next(); if (tried.contains(next)) { continue; } count++; tried.add(next); try { ObjectNode params = JOM.createObjectNode(); params.set("me", JOM.getInstance().valueToTree(rt.getMyKey())); params.set("near", JOM.getInstance().valueToTree(near)); AsyncCallback<ObjectNode> callback = new AsyncCallback<ObjectNode>() { @Override public void onSuccess(ObjectNode res) { List<Node> result = NODELIST.inject(res.get("nodes")); rt.seenNode(next); synchronized (shortList) { insert(shortList, near, result); noInFlight[0]--; shortList.notifyAll(); } } @Override public void onFailure(Exception exception) { synchronized (shortList) { shortList.remove(near.dist(next.getKey())); noInFlight[0]--; shortList.notifyAll(); LOG.log(Level.WARNING, noInFlight[0] + ":OnFailure called:" + next.getUri(), exception); } } }; caller.call(next.getUri(), "dht.find_close_nodes", params, callback); synchronized (shortList) { noInFlight[0]++; } } catch (IOException e) { synchronized (shortList) { shortList.remove(near.dist(next.getKey())); } continue; } } if (count == 0) { keepGoing = false; } } synchronized (shortList) { return new ArrayList<Node>(shortList.values()); } }
From source file:com.almende.dht.rpc.DHT.java
/** * Iterative_find_value./*from ww w.java 2 s . c o m*/ * * @param key * the key * @param multiple * the multiple * @return the object node */ public JsonNode iterative_find_value(final Key key, final boolean multiple) { final JsonNode[] result = new JsonNode[1]; final TreeMap<Key, Node> shortList = new TreeMap<Key, Node>(); final Set<Node> tried = new HashSet<Node>(); insert(shortList, key, rt.getClosestNodes(key, Constants.A)); final int[] noInFlight = new int[1]; noInFlight[0] = 0; boolean keepGoing = true; while (keepGoing) { while (noInFlight[0] > 0 && result[0] == null) { synchronized (shortList) { try { shortList.wait(100); } catch (InterruptedException e) { } } } List<Node> copy = Arrays.asList(shortList.values().toArray(new Node[0])); Collections.sort(copy); final Iterator<Node> iter = copy.iterator(); int count = 0; while (iter.hasNext() && count < Constants.A) { final Node next = iter.next(); if (tried.contains(next)) { continue; } count++; tried.add(next); try { ObjectNode params = JOM.createObjectNode(); params.set("me", JOM.getInstance().valueToTree(rt.getMyKey())); params.set("key", JOM.getInstance().valueToTree(key)); params.put("multiple", multiple); AsyncCallback<ObjectNode> callback = new AsyncCallback<ObjectNode>() { @Override public void onSuccess(ObjectNode res) { if (res.has("value")) { result[0] = (ObjectNode) res.get("value"); } else if (res.has("values")) { result[0] = (ArrayNode) res.get("values"); } else { List<Node> nodes = NODELIST.inject(res.get("nodes")); synchronized (shortList) { insert(shortList, key, nodes); } } rt.seenNode(next); synchronized (shortList) { noInFlight[0]--; shortList.notifyAll(); } } @Override public void onFailure(Exception exception) { synchronized (shortList) { shortList.remove(key.dist(next.getKey())); noInFlight[0]--; shortList.notifyAll(); LOG.log(Level.WARNING, noInFlight[0] + ":OnFailure called:" + next.getUri(), exception); } } }; caller.call(next.getUri(), "dht.find_value", params, callback); synchronized (shortList) { noInFlight[0]++; } } catch (IOException e) { synchronized (shortList) { shortList.remove(key.dist(next.getKey())); } continue; } } if (count == 0) { keepGoing = false; } } if (result[0] == null) { return JOM.createNullNode(); } else { return result[0]; } }