This is synchronized to ensure that we process the queue serially.
354144
private synchronized void complete(String message) {
    
    // This is a weird problem with writing stuff while idling. Need to investigate it more, but
    // for now just ignore it.
    if (MESSAGE_COULDNT_BE_FETCHED_REGEX.matcher(message).matches()) {
      log.warn("Some messages in the batch could not be fetched for {}\n" +
          "---cmd---\n{}\n---wire---\n{}\n---end---\n", new Object[] {
          config.getUsername(),
          getCommandTrace(),
          getWireTrace()
      });
      errorStack.push(new Error(completions.peek(), message, wireTrace.list()));
      final CommandCompletion completion = completions.peek();
      String errorMsg = "Some messages in the batch could not be fetched for user " + config.getUsername();
      RuntimeException ex = new RuntimeException(errorMsg);
      if (completion != null) {
        completion.error(errorMsg, new MailHandlingException(getWireTrace(), errorMsg, ex));
        completions.poll();
      } else {
        throw ex;
      }
    }

    CommandCompletion completion = completions.peek();
    if (completion == null) {
      if ("+ idling".equalsIgnoreCase(message)) {
        synchronized (idleMutex) {
          idler.idleStart();
          log.trace("IDLE entered.");
          idleAcknowledged.set(true);
        }
      } else {
        log.error("Could not find the completion for message {} (Was it ever issued?)", message);
        errorStack.push(new Error(null, "No completion found!", wireTrace.list()));
      }
      return;
    }
    
    if (completion.complete(message)) {
      completions.poll();
    }
  }
1223988
def process_queues(self, forced=False):
        """"""

        with self.lock:
            if (not forced and not self.must_process) or not self.queues_enabled:
                return
            self.must_process = False
        ARBITRATOR.awaken()
1040906
private void queueProcessable(final IEngineProcessable processableModel) {

        ensurePendingCapacity();

        final TemplateFlowController controller = this.flowController;

        this.pendingProcessings[this.pendingProcessingsSize] = processableModel;
        this.pendingProcessingsSize++;

        if (controller.stopProcessing) {
            controller.processorTemplateHandlerPending = true;
            return;
        }

        final boolean processed = this.pendingProcessings[this.pendingProcessingsSize - 1].process();
        if (!processed) {
            controller.processorTemplateHandlerPending = true;
            return;
        }
        this.pendingProcessingsSize--;

        controller.processorTemplateHandlerPending = false;

    }
91903
function Queue(concurrency) {
  this.paused = false;
  this.concurrency = concurrency;
  this.waitTimeout = 0;
  this.processTimeout = 0;
  this.throttleCount = 0;
  this.throttleInterval = 1000;
  this.count = 0;
  this.tasks = [];
  this.waiting = [];
  this.factors = {};
  this.fifoMode = true;
  this.roundRobinMode = false;
  this.priorityMode = false;
  this.onProcess = null;
  this.onDone = null;
  this.onSuccess = null;
  this.onTimeout = null;
  this.onFailure = null;
  this.onDrain = null;
}
576194
void setStrongMatchWaitLock() {
        synchronized (reqQueueLockObject) {
            for (ServerRequest req : queue) {
                if (req != null) {
                    if (req instanceof ServerRequestInitSession) {
                        req.addProcessWaitLock(ServerRequest.PROCESS_WAIT_LOCK.STRONG_MATCH_PENDING_WAIT_LOCK);
                    }
                }
            }
        }
    }
23033
private void onBegin()
   {
      Queue<Long> q = currentTime.get();
      q.add(System.currentTimeMillis());
   }
1232135
public T sync() {
        //if data is present there's no need to block
        if (data != null) {
            return data;
        }
        synchronized (this) {
            try {
                // wait();
                block.take();
            } catch (InterruptedException e) {
                if (interruptCause == null) {
                    interruptCause = e;
                }
            }
            if (interruptCause != null) {
                if (interruptCause instanceof DataSiftException) {
                    throw (DataSiftException) interruptCause;
                } else {
                    throw new DataSiftException("Interrupted while waiting for response", interruptCause);
                }
            }
            return data;
        }
    }
1368682
@Override
	protected void consume() {
		E obj = null;
		try {
			obj = queue.take();
		} catch (InterruptedException e) {
			//thread.isInterrupted();
			return;
		}
		process(obj);
	}
114501
boolean enqueue(Reference<? extends T> reference) {
        synchronized (lock) {
            if (enqueueLocked(reference)) {
                lock.notifyAll();
                return true;
            }
            return false;
        }
    }
836499
protected synchronized boolean isBackgroundInvalidationInProgress() {
        boolean cleanupThreadRunning = false;
        if (this.cod.diskCleanupThread != null) {
            synchronized (cod.diskCleanupThread.dcMonitor) {
                cleanupThreadRunning = this.cod.diskCleanupThread.currentThread != null;
            }
        }
        boolean garbageCollectorThreadRunning = false;
        if (this.cod.garbageCollectionThread != null) {
            synchronized (cod.garbageCollectionThread.gcMonitor) {
                garbageCollectorThreadRunning = this.cod.garbageCollectionThread.currentThread != null;
            }
        }

        return (cleanupThreadRunning || garbageCollectorThreadRunning) ? true : false;
    }
1295960
private void ensureWatchThreadRunning() {
		if (!threadRunning) {
			thread = new Thread(watchThread);
			thread.setDaemon(true);
			thread.start();
			watchThread.setThread(thread);
			watchThread.updateName();
			threadRunning = true;
		}
	}
1335611
private final void contendedLock() {
            boolean waiting = false;
            for (int s;;) {
                if (((s = lockState) & ~WAITER) == 0) {
                    if (U.compareAndSwapInt(this, LOCKSTATE, s, WRITER)) {
                        if (waiting)
                            waiter = null;
                        return;
                    }
                }
                else if ((s & WAITER) == 0) {
                    if (U.compareAndSwapInt(this, LOCKSTATE, s, s | WAITER)) {
                        waiting = true;
                        waiter = Thread.currentThread();
                    }
                }
                else if (waiting)
                    LockSupport.park(this);
            }
        }
254591
public synchronized boolean isRunning() {
        if (null == process) return false;

        try {
            process.exitValue();
            return false;
        } catch(IllegalThreadStateException itse) {
            return true;
        }
    }
605797
public synchronized void flush()
	{
		if (out != null)
			try {
				out.flush();
			}
			catch (final IOException e) {
				getErrorHandler().error(this, "on flush", e);
			}
	}
408449
public void flush()
  {
    if (!enabled)
    {
      return;
    }
    if (!queue.isEmpty())
    {
      // start a new thread to upload without blocking the current thread
      Runnable runUpload = new TelemetryUploader(this, exportQueueToString());
      uploader.execute(runUpload);
    }
  }
1018764
private void emitLoop() {
        for (;;) {
            AppendOnlyLinkedArrayList<T> q;
            synchronized (this) {
                q = queue;
                if (q == null) {
                    emitting = false;
                    return;
                }
                queue = null;
            }
            q.accept(actual);
        }
    }
835800
protected void sync(AbstractByteArrayBufferBuffer master) {

    this.buffersEndIndex = master.buffersEndIndex;
    int bufferDist = master.bufferStepCount - this.bufferStepCount;
    if (bufferDist > 0) {
      this.buffersIndex = master.buffersIndex;
      this.bufferStepCount = master.bufferStepCount;
      ByteArrayBuffer buffer = this.buffers[this.buffersIndex];
      this.currentBufferBytes = buffer.getBytes();
      this.currentBufferIndex = buffer.getCurrentIndex();
      this.currentBufferMax = buffer.getMaximumIndex();
    }
  }