code: private HeapState processHeapStateCreation(final ConnectedResponse result, final String heapUri) {
          if (!subTermLock.isHeldByCurrentThread()) {
              throw new IllegalStateException("You must have the subTermLock before calling this method");
          }
          final HeapState newState = new HeapState(result.getHeap());
          // we're safe to lock this out of normal order as the HeapState isn't visible to other threads until the
          // end of this block. We have to have the lock before we make it visible..
          newState.getUpdateLock().lock();
          // new heap for this transport
          UpdateProducingHeapListener listener = new UpdateProducingHeapListener() {
  
              @Override
              protected void doUpdate(Update u) {
                  if (u.getActions().size() > 0) {
                      newState.getQueuedChanges().add(new QueuedHeapChange(u));
                      // bad luck, we just added the heap and it's just about to get terminated...
                      if (u.getActions().contains(TerminateHeap.INSTANCE)) {
                          newState.getQueuedChanges().add(new HeapTermination());
                      }
                      heapsWaitingForUpdate.add(heapUri);
                  }
              }
          };
          newState.setHeapListener(listener);
          result.getHeap().addListener(listener, false);
          heapStates.put(heapUri, newState);
          heapUris.put(newState.getHeapId(), heapUri);
          return newState;
      }
docs: note, you must have the subterm lock before calling this method