sclass AllOnAllWithUpdates { new TreeMap aList; new TreeMap bList; LPair todo = new LinkedList; LPair todo2 = new LinkedList; synchronized void newA(A a) { aList.put(or0(lastKey(aList))+1, a); addPair(todo, longRange_one(lastKey(aList)), longRange(0, lastKey(bList)+1)); } synchronized void newB(B b) { bList.put(or0(lastKey(bList))+1, b); addPair(todo, longRange(0, lastKey(aList)+1), longRange_one(lastKey(bList))); } synchronized Pair next() { while licensed { while (empty(todo2)) { if (empty(todo)) null; Pair p = popFirst(todo); addAll(todo2, outerProduct_pairs(keys(subMap_longRange(aList, p.a)), keys(subMap_longRange(bList, p.b)))); } Pair p = popFirst(todo2); A a = aList.get(p.a); B b = bList.get(p.b); if (a != null && b != null) ret pair(a, b); } null; } }