Running this code snippet from scala interpreter results in a deadlock: (run as selection, all at once)
val foo: String = "Foo";
val r = new Runnable {
def run(): Unit = {
println(s"Hi $foo"); //hangs interpreter
}
};
val t1 = new Thread(r);
t1.start(); t1.join(); // no printout, intepreter deadlock
println("done...");
Leave a comment
on 2014-08-06 09:23 *
By Iulian Dragos
Description changed from Running this code snippet f... to Running this code snippet f...
Here's the relevant stack trace. It's an artifact of how the REPL compiles code (it's run in the object constructor, which is run during the static initializer. The reference to `foo`, a field of the same object, needs to wait for the static initializer, but that one blocked at the `join` call.
Open for suggestions on how to fix it, but I doubt we can make this safe.
As a workaround, you can either enter the statements separately, or surround them with braces.
Open for suggestions on how to fix it, but I doubt we can make this safe.
As a workaround, you can either enter the statements separately, or surround them with braces.
"Thread-13" #121 daemon prio=6 os_prio=31 tid=0x0000000100a40000 nid=0x12003 in Object.wait() [0x000000012e90e000]
java.lang.Thread.State: RUNNABLE
at $line1.$read$$iw$$iw$$anon$1.run(<console>:11)
at java.lang.Thread.run(Thread.java:745)
"ForkJoinPool-2-worker-29" #120 daemon prio=6 os_prio=31 tid=0x00000001231eb000 nid=0x11603 in Object.wait() [0x000000012cdfe000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at java.lang.Thread.join(Thread.java:1245)
- locked <0x000000078e7ef918> (a java.lang.Thread)
at java.lang.Thread.join(Thread.java:1319)
at $line1.$read$$iw$$iw$.<init>(<console>:16)
at $line1.$read$$iw$$iw$.<clinit>(<console>)
at $line1.$eval$.$print$lzycompute(<console>:7)
- locked <0x000000078e7d9308> (a $line1.$eval$)
at $line1.$eval$.$print(<console>:6)
at $line1.$eval.$print(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:739)
at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:986)
No file chosen
You have an empty file field. Please select or remove it.
Name | Size |
---|