Skip to content

Instantly share code, notes, and snippets.

@mariusae
Forked from nkallen/gist:641482
Created October 22, 2010 22:27
Show Gist options
  • Save mariusae/641496 to your computer and use it in GitHub Desktop.
Save mariusae/641496 to your computer and use it in GitHub Desktop.
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.atomic.AtomicInteger
object Hotpotato {
private val nwaiters = new AtomicInteger(0)
private val executionQueue = new LinkedBlockingQueue[Function0[Unit]]
def serialized(f: => Unit) {
executionQueue offer { () => f }
if (nwaiters.getAndIncrement() == 0) {
do {
val task = executionQueue.poll()
assert(task ne null)
task()
} while (nwaiters.decrementAndGet() > 0)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment