private SegmentActions advanceTo(int value) {
      synchronized (ArrayLongLongMapMmap.this) {
        currentSeg.set(value);
        SegmentActions result = new SegmentActions();
        var min = segments.stream().mapToInt(AtomicInteger::get).min().orElseThrow();
        if (min == Integer.MAX_VALUE) {
          // all workers are done, flush everything
          result.flush.addAll(writeBuffers.values());
          writeBuffers.clear();
          tail = min;
        } else if (value == Integer.MAX_VALUE) {
          // this worker is done, advance tail to min
          for (Integer key : writeBuffers.keySet()) {
            if (key < min) {
              var segment = writeBuffers.remove(key);
              if (segment != null) {
                result.flush.add(segment);
              }
            }
          }
          tail = min;
        } else {
          // if the tail segment just finished, then advance the tail and flush all pending segments
          while (tail < min) {
            if (writeBuffers.containsKey(tail)) {
              var segment = writeBuffers.remove(tail);
              if (segment != null) {
                result.flush.add(segment);
              }
            }
            tail++;
          }
          Segment segment = writeBuffers.computeIfAbsent(value, id -> {
            var seg = new Segment(id);
            result.allocate.add(seg);
            return seg;
          });
          result.setResult(segment);
        }

        // let workers waiting to allocate new segments to the head of the sliding window proceed
        // NOTE: the memory hasn't been released yet, so the activeChunks semaphore will cause
        // those workers to wait until the memory has been released.
        slidingWindow.advanceTail(tail);
        return result;
      }
    }
Handle for a single worker thread to write values in parallel with other workers.
planetiler-core/src/main/java/com/onthegomap/planetiler/collection/ArrayLongLongMapMmap.java