Skip to content

Instantly share code, notes, and snippets.

@focusj
Created January 20, 2014 16:43
Show Gist options
  • Save focusj/8523701 to your computer and use it in GitHub Desktop.
Save focusj/8523701 to your computer and use it in GitHub Desktop.
for test
package sample.remote.test
import scala.actors.threadpool.Executor
import scala.actors.threadpool.Executors
import java.util.concurrent.ExecutorService
class Test1 {
private val map = scala.collection.mutable.HashMap[Int, String]()
def get(index: Int) = synchronized {
map.getOrElse(index, "empty")
}
def set(index: Int, content: String) =
{
synchronized {
map += (index -> content)
}
}
def size = map.size
}
class Test2 {
private var map = Map[Int, String]()
def get(index: Int) = {
map.getOrElse(index, "empty")
}
def set(index: Int, content: String) =
{
synchronized {
map = map + (index -> content)
}
}
def size = map.size
}
object Test1 extends App {
// val t = new Test1()
val t = new Test2()
val exec = Executors.newFixedThreadPool(500)
val start = System.currentTimeMillis()
for (i <- 0 to 1000000) {
exec.execute(new Runnable {
override def run() {
t.set(i, s"content is $i")
t.get(i-1)
}
})
}
while(t.size < 1000000) {
}
println(System.currentTimeMillis() - start)
exec.shutdown()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment