1

Hi I wonder how to measure thread pool time in scala.

Here is a example.

val pool = java.util.concurrent.Executors.newFixedThreadPool(2)

val start_time = System.nanoTime()
1 to 10 foreach { x =>
  pool.execute(
    new Runnable {
      def run {
        try{
            Thread.sleep(2000)
            println("n: %s, thread: %s".format(x, Thread.currentThread.getId))
        }finally{
            pool.shutdown()
        }
      }
    }
  )
}
val end_time = System.nanoTime()

println("time is "+(end_time - start_time)/(1e6 * 60 * 60))

But I think this is not working properly.

Is there any methods to measure the time?

S.Kang
  • 581
  • 2
  • 10
  • 28

1 Answers1

2

There are numerous threads in your snippet.

  • main thread where you've created fixed thread pool and execute the loop.
  • 10 threads that are "sleeping" 2 seconds and printing some stuff.

Your main thread as soon as it create 10 threads finish its work and print time. It does not wait for all parallel threads to be completed. What you have to do is await results from all threads and only then perform total time calculation.

I would suggest you to learn a bit about concept of Future which will allow you to wait for result properly.

So your code might looks like following:

    import scala.concurrent.ExecutionContext.Implicits.global
  import scala.concurrent._
  import scala.concurrent.duration.Duration

val start_time = System.nanoTime()
val zz = 1 to 10 map { x =>
  Future {
    Thread.sleep(2000)
    println("n: %s, thread: %s".format(x, Thread.currentThread.getId))
  }
}
Await.result(Future.sequence(zz), Duration.Inf)

val end_time = System.nanoTime()

println("time is " + (end_time - start_time) / (1e6 * 60 * 60))

I've used default global scala thread pool.

vvg
  • 6,325
  • 19
  • 36
  • Thanks for your reply! Could you answer one more question? If you don't mind? When I use your code in spark-shell, I got error like this. `error: object java.util.concurrent.Future is not a value Future { ` and `error: not found: value Duration Await.result(Future.sequence(zz), Duration.Inf)` what is the Future value? – S.Kang Jan 24 '17 at 14:48
  • Do I import `import scala.concurrent.Future`?? – S.Kang Jan 24 '17 at 14:51
  • added imports to the sample. should work fine in any scala console. – vvg Jan 24 '17 at 14:53
  • Thanks! It's worked! This is really my last question, Is it impossible to measure running time using thread pool? – S.Kang Jan 24 '17 at 14:58
  • I assume by "using thread pool" you mean thread pool created by Java API instead of Scala API. Yes it's possible - learn how to use `Future` in Java and use the same concept to wait for results but all in Java http://stackoverflow.com/questions/19348248/waiting-on-a-list-of-future – vvg Jan 24 '17 at 15:00
  • I appreciate your advice! I'll try it! – S.Kang Jan 24 '17 at 15:02