Skip to content

Instantly share code, notes, and snippets.


Created Jul 20, 2017
What would you like to do?
the description for this gist
private def getQueueForRunnable(command: Runnable) = {
val runnableHash = command.hashCode()
def sbhash(i: Int) = reverseBytes(i * 0x9e3775cd) * 0x9e3775cd
def getNext = executionCounter.incrementAndGet() % parallelism
def updateIfAbsentAndGetQueueIndex(
workerQueueIndex: AtomicReference[ImmutableIntMap],
runnableHash: Int, queueIndex: Int): Int = {
def updateIndex(): Unit = {
val prev = workerQueueIndex.get()
if (!runnableToWorkerQueueIndex.
compareAndSet(prev, prev.updateIfAbsent(runnableHash, queueIndex))) {
workerQueueIndex.get().get(runnableHash) // can safely call get..
val workQueueIndex =
if (fairDistributionThreshold == 0
|| runnableToWorkerQueueIndex.get().size > fairDistributionThreshold)
Math.abs(sbhash(runnableHash)) % parallelism
updateIfAbsentAndGetQueueIndex(runnableToWorkerQueueIndex, runnableHash, getNext)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.