-
-
Save mariusae/641496 to your computer and use it in GitHub Desktop.
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
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