File: t6969.scala

package info (click to toggle)
scala 2.11.12-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 62,924 kB
  • sloc: javascript: 28,808; java: 13,415; xml: 3,135; sh: 1,620; python: 756; makefile: 38; awk: 36; ansic: 6
file content (32 lines) | stat: -rw-r--r-- 925 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32


import scala.language.{ reflectiveCalls }

object Test {
  private type Clearable = { def clear(): Unit }
  private def choke() = {
    try new Array[Object]((Runtime.getRuntime().maxMemory min Int.MaxValue).toInt)
    catch {
      case _: OutOfMemoryError => // what do you mean, out of memory?
      case t: Throwable        => println(t)
    }
  }
  private def f(x: Clearable) = x.clear()
  class Choker(id: Int) extends Thread {
    private def g(iteration: Int) = {
      val map = scala.collection.mutable.Map[Int, Int](1 -> 2)
      try f(map) catch { case t: NullPointerException => println(s"Failed at $id/$iteration") ; throw t }
      choke()
    }
    override def run() {
      1 to 50 foreach g
    }
  }

  def main(args: Array[String]): Unit = {
    val threads = 1 to 3 map (id => new Choker(id))
    threads foreach (_.start())
    threads foreach (_.join())
    println("All threads completed.")
  }
}