Created
July 20, 2017 06:42
-
-
Save anonymous/96a4cb469274d4c161bb8650283bc7c7 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
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