Skip to content

Threads and Executors

Florian Schmaus edited this page Aug 5, 2015 · 11 revisions

Current Threads/Executors

AbstractXMPPConnection

  • executorService (ThreadPoolExecutor, maxSize=1) used to invoke collectors and eventually a singleThreadedExecutor for listeners, has a queue size of 100
  • removeCallbacksService (ScheduledExecutorService, maxSize=1) used to remove pending callbacks after a timeout
  • cachedExecutorService (CachedThreadPool) used for Smack's asyncGo() API, which includes async listeners
  • singleThreadedExecutorService (SingleThreadExecutor, maxSize=1) used to invoke listeners, TODO queue size?

XMPPTCPConnection

  • reader thread
  • writer thread

PingManager

  • executorService (SingleThreadedScheduledExecutor) used to schedule server pings

Thoughts

  • XMPPTCPConnection could use NIO and therefore use a single thread
  • sharing cachedExecutorService accross connections yields no benefit, as it's min core pool size is 0
  • remove callbacks service could eventually be shared
  • executorService's main (/only?) purpose is being a buffer, i.e. its queue. The other reason it exists is that it's core pool size is one, i.e. there is always one thread ready for work

MultipleQueueNonConcurrentExecutionThreadSharingExecutor

put(Runnable runnable, int queueID) {
  queues[queueID].put(runnable)
  if (!inExecution[queueId] && currentWorkers < maxWorkers) {
    // create new worker
  }
}

workerLoop() {
  Runnable work = null;
  for (int i = 0; i < maxQueues; i++)
    if (queues[i].isEmpty()) continue;
    if (inExecution[i]) continue;
    work = queues[i].pop();
    inExecution[i] = true;
    work();
    inExecution[i] = false;
  }
  if (noNewWorkAddedSinceBeginningOfLoop)
    if (currentWorkers > minWorkers) {
     currentWorkers--;
     return;
    } else {
       // sleep until new work
    }
  }
}