Created
July 20, 2017 06:42
-
-
Save anonymous/a7c9191186d97303b737d9f8ac52a56c to your computer and use it in GitHub Desktop.
the description for this gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = { | |
@tailrec | |
def updateIndex(): Unit = { | |
val prev = workerQueueIndex.get() | |
if (!runnableToWorkerQueueIndex. | |
compareAndSet(prev, prev.updateIfAbsent(runnableHash, queueIndex))) { | |
updateIndex() | |
} | |
} | |
updateIndex() | |
workerQueueIndex.get().get(runnableHash) // can safely call get.. | |
} | |
val workQueueIndex = | |
if (fairDistributionThreshold == 0 | |
|| runnableToWorkerQueueIndex.get().size > fairDistributionThreshold) | |
Math.abs(sbhash(runnableHash)) % parallelism | |
else | |
updateIfAbsentAndGetQueueIndex(runnableToWorkerQueueIndex, runnableHash, getNext) | |
workQueues(workQueueIndex) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment