Skip to content

Instantly share code, notes, and snippets.

Created July 20, 2017 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/96a4cb469274d4c161bb8650283bc7c7 to your computer and use it in GitHub Desktop.
Save anonymous/96a4cb469274d4c161bb8650283bc7c7 to your computer and use it in GitHub Desktop.
the description for this gist
class UserServiceActor(userDb: Map[Int, User],
latch: CountDownLatch,
numQueries: Int) extends Actor {
private var left = numQueries
def receive = {
case Request(id) =>
userDb.get(id) match {
case Some(u) => sender() ! u
case None =>
}
if (left == 0) {
latch.countDown()
context stop self
}
left -= 1
}
}
class UserQueryActor(latch: CountDownLatch,
numQueries: Int,
numUsersInDB: Int) extends Actor {
private var left = numQueries
private val receivedUsers: mutable.Map[Int, User] = mutable.Map()
private val randGenerator = new Random()
override def receive: Receive = {
case u: User => {
receivedUsers.put(u.userId, u)
if (left == 0) {
latch.countDown()
context stop self
} else {
sender() ! Request(randGenerator.nextInt(numUsersInDB))
}
left -= 1
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment