执行程序运行时间超过超时值

use*_*384 0 timeout scala executors

这是scala的代码段.我将超时设置为100毫克.在10000个循环中,其中106个循环超过100个,没有抛出异常.最大的一个甚至是135个.出现这种情况的原因是什么?

for (j <- 0 to 10000) {
  total += 1
  val executor = Executors.newSingleThreadExecutor
  val result = executor.submit[Int](new Callable[Int] {
      def call = try {
        Thread.sleep(95)
        for (i <- 0 to 1000000) {}
        4   
      } catch {
        case e: Exception => exception1 += 1
        5   
      }   
  })  

  try {
    val t1 = Calendar.getInstance.getTimeInMillis
    result.get(100, TimeUnit.MILLISECONDS)
    val t2 = Calendar.getInstance.getTimeInMillis
    println("timediff = " + (t2 - t1).toString)
  } catch {
    case e: Exception => exception2 += 1
  }   
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*ise 6

首先,如果您在Windows上运行,您应该知道计时器分辨率大约是15.6毫秒.

其次,1M迭代的空循环很可能被编译器删除,更重要的是,不能被任何超时中断.